JavaScript counts characters in Textarea
Posted: Fri Jun 02, 2006 11:36 am
The following code is used to count the number of characters typed into a textarea and count down from the 480 maximum allowed characters. The form where the textarea appears is posted to a confirmation page. There is a submit button and an edit button on the confirmation page. When the user chooses to edit the entry, this script does NOT maintain the number of characters remaining of the allowable 480 characters.
Does any one have any ideas on how to fix the code so that the number of remaining characters is carried through and additions or deletions made in the edit form are reflected?
Any thought will be gratefully received. 
Does any one have any ideas on how to fix the code so that the number of remaining characters is carried through and additions or deletions made in the edit form are reflected?
Code: Select all
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}
<textarea id="message" name="message" wrap="hard" cols=70
rows=10 width="120" onKeyDown="textCounter(this.form.message,this.form.remLen,480);"
onKeyUp="textCounter(this.form.message,this.form.remLen,480);"><?php print (htmlspecialchars(ltrim(stripslashes($message)))) ?></textarea>
<br><br>
You have <input readonly type=text name=remLen size=3 maxlength=3
value="480"> characters left