count chars?

R

roN

Hey,

I got following:
HTML:
<form name="Formular" method="post" onSubmit="return chkFormular()"
action="mail.php" enctype="text/plain">
....
....
....
<td align="right" valign="top" class="text"><font
color='red'>*</font>Comments/Questions:<br>
<input name="input" size="1"
style="border-width:0;background-color:#FFFFFF"
onfocus="if(this.blur)this.blur()">
chars left</td>
<td class="text"> <textarea name="comments"
onkeyup="count(event)" rows="8" cols="40" class="flat"></textarea>
</td>
[js]
<script language="JavaScript">
var max = 255;

document.Formular.input1.value = max;
document.Formular.comments.focus();

function count(e) {
if (!e.which) keyCode = event.keyCode; // ie5+ op5+
else keyCode = e.which; // nn6+

if (document.Formular.comments.value.length<max+1)
document.Formular.input1.value =
max-document.Formular.comments.value.length;
else {
document.Formular.comments.value =
document.Formular.comments.value.substring(0,max);
document.Formular.input1.value = 0;
}
}
</script>
[/js]
That's supposed to display how many chars are left besides the textarea,
decreasing from 255. But it doesn't display anything, why not? Thank you
guys!
 
R

RobG

roN said:
Hey,

I got following:
HTML:
<form name="Formular" method="post" onSubmit="return chkFormular()"
action="mail.php" enctype="text/plain">
...
...
...
<td align="right" valign="top" class="text"><font
color='red'>*</font>Comments/Questions:<br>
<input name="input" size="1"
style="border-width:0;background-color:#FFFFFF"
onfocus="if(this.blur)this.blur()">
chars left</td>
<td class="text"> <textarea name="comments"
onkeyup="count(event)" rows="8" cols="40" class="flat"></textarea>
</td>
[js]
<script language="JavaScript">
var max = 255;

document.Formular.input1.value = max;

The script console told me that document.Formular.input1 has no
properties. That is because your input has a name of 'input'. Change
it to input1 and it 'works'.

document.Formular.comments.focus();

function count(e) {
if (!e.which) keyCode = event.keyCode; // ie5+ op5+
else keyCode = e.which; // nn6+

if (document.Formular.comments.value.length<max+1)
document.Formular.input1.value =
max-document.Formular.comments.value.length;
else {
document.Formular.comments.value =
document.Formular.comments.value.substring(0,max);
document.Formular.input1.value = 0;
}

That appears to be an extremely lengthy way to go about things. Why not
change the call to:

<textarea name="comments" onkeyup="count(this)" ... >


And the function to:

function count(el)
{
document.Formular.input1.value = max - el.value.length;
}

[...]
 
T

Thomas 'PointedEars' Lahn

RobG said:
That appears to be an extremely lengthy way to go about things.
Why not change the call to:

<textarea name="comments" onkeyup="count(this)" ... >


And the function to:

function count(el)
{
document.Formular.input1.value = max - el.value.length;

document.forms['Formular'].elements['input1'].value =
max - el.value.length;

is (almost) standards compliant (apart from `document') and backwards
compatible.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top