JavaScript counts characters in Textarea

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
penpaper
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 6:45 pm
Location: Coudersport, PA, USA

JavaScript counts characters in Textarea

Post 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. :)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Moved to client side
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Post Reply