...

View Full Version : TextBox TextArea Limit



angiras
10-27-2003, 09:16 AM
The following code is working fine

------------------------------------------------------

<script type="text/javascript">
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
{field.value = field.value.substring(0, maxlimit);}
else
{countfield.value = maxlimit - field.value.length;}
}
</script>

<textarea id="A" onKeyDown="textCounter(this.form.A,this.form.A_read,100);" onKeyUp="textCounter(this.form.A,this.form.A_read,100);" ></textarea>

<input type="Text" readonly id="A_read" value="100"/>

---------------------------------------------------------

but how can I do it in an easier way ? in that way >>>

--------------------------------------------------------


<script type="text/javascript">
function textCounter(field, maxlimit) {
if (field.value.length > maxlimit)
{field.value = field.value.substring(0, maxlimit);}
else
{field+"_read".value = maxlimit - field.value.length;}
}
</script>

<textarea id="A" onKeyDown="textCounter(this,100);" onKeyUp="textCounter(this,100);"></textarea>
<input type="Text" readonly id="A_read" value="100" />

-----------------------------------------------

thank you

glenngv
10-27-2003, 09:25 AM
field.form.elements[field.name+"_read"].value = maxlimit - field.value.length;

angiras
10-27-2003, 09:48 AM
thanks glenngv !
it works with

[field.name+"_read"].value = maxlimit - field.value.length;

and not with

field.form.elements[field.name+"_read"].value = maxlimit - field.value.length


but the textBox A_read now doesn't show the numbers it keeps 100

glenngv
10-27-2003, 10:06 AM
huh? it works with:

[field.name+"_read"].value = maxlimit - field.value.length;

:confused:

maybe this doesn't work:

field.form.elements[field.name+"_read"].value = maxlimit - field.value.length

because you are using id instead of name in the field.

ok, try this.

document.getElementById(field.name+"_read").value = maxlimit - field.value.length;

angiras
10-27-2003, 10:13 AM
all the code >>>>


<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Text Box limit</title>
<script type="text/javascript">
function textCounter(field, maxlimit)
{
if (field.value.length > maxlimit)
{
field.value = field.value.substring(0, maxlimit);
}
else
{
document.getElementById(field.name+"_read").value = maxlimit - field.value.length;
}
}
</script>
</head>
<body>
<form id="MainForm">

<textarea id="A" onKeyDown="textCounter(this,100);" onKeyUp="textCounter(this,100);"></textarea>

<input type="Text" readonly id="A_read" value="100"/>

</form>
</body>
</html>


--------------------------------

and I get the message

document.getElementById(....) is not an object



:-((

glenngv
10-27-2003, 10:23 AM
ok, use field.id instead of field.name

document.getElementById(field.id+"_read").value = maxlimit - field.value.length;

also try the original code:

field.form.elements[field.id+"_read"].value = maxlimit - field.value.length

it might work now.

angiras
10-27-2003, 10:36 AM
it works !!

both are working

field.form.elements[field.id+"_read"].value = maxlimit - field.value.length

OR

document.getElementById(field.id+"_read").value = maxlimit - field.value.length;


thank you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum