Page 1 of 1

JavaScript counts characters in Textarea

Posted: Fri Jun 02, 2006 11:36 am
by penpaper
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?

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 &nbsp;<input readonly type=text name=remLen size=3 maxlength=3 
value="480"> &nbsp;characters left
Any thought will be gratefully received. :)

Posted: Fri Jun 02, 2006 11:43 am
by Burrito
Moved to client side

Posted: Fri Jun 02, 2006 11:43 am
by Burrito
fire an onLoad event to determine how many chars are in the box and then you can figure out how many would be remaining.