Question : Limit number of chars in a multiple textarea form

Hi,

I am using a piece of javascript I found to limit the number of characters a user can input into a textarea. It seems to work ok but I can only have one instance per form.

If I add a second text area then neither will be limited and the character count doesn't work.

I need to have multiple (around a dozen) text areas in my form and want to limit each one to 200 chars.

Not being a javascript expert I would like to know if there is something I can tweak to have this work multiple times in the same form.

The script is:

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
      if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
      } else {
            limitCount.value = limitNum - limitField.value.length;
      }
}
</script>

And the form contains:

<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,100);"
onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,100);">
</textarea><br>
<font size="1">(Maximum characters: 100)<br>
You have <input readonly type="text" name="countdown" size="3" value="100"> characters left.</font>

I have tried changing the name "limitedtextarea" and also change this where is is mentioned in the function call. But to no avail. All instances of the count then fail. As I say, I'm no expert here and I'm sure it's fairly simple.

Can anyone help?

Thanks
RJ

Answer : Limit number of chars in a multiple textarea form

You need to pair each textarea to it's input that presents the count of chars left.
You normally need only keyup or down, not both.

<textarea name="limitedtextarea1" onKeyUp="limitText(this,this.form.countdown1,200);">
</textarea><br>
<font size="1">(Maximum characters: 200)<br>
You have <input readonly type="text" name="countdown1" size="3" value="200"> characters left.</font>

<textarea name="limitedtextarea2" onKeyUp="limitText(this,this.form.countdown2,200);">
</textarea><br>
<font size="1">(Maximum characters: 200)<br>
You have <input readonly type="text" name="countdown2" size="3" value="200"> characters left.</font>
Random Solutions  
 
programming4us programming4us