3

I would like to count characters in a form textarea field dynamically, I know there is a similar question about this but I am wondering if it can count from a certain number towards 0, as example while writing a tweet on Twitter.

This code could be in javascript, PHP, it doesn't really matter to me.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97
  • possible duplicate of [jQuery - Count characters in textarea](http://stackoverflow.com/questions/5371089/jquery-count-characters-in-textarea) – Gabriele Petrioli Mar 21 '11 at 14:51

2 Answers2

6

This should help you out. Here is a tutorial regarding usage - a demo is found below:

Usage:

<input name="text" onKeyDown="CountLeft(this.form.text, this.form.left,50);"
                   onKeyUp="CountLeft(this.form.text,this.form.left,50);">

Javascript:

<SCRIPT LANGUAGE="JavaScript">

 function CountLeft(field, count, max) 
 {
     if (field.value.length > max)
         field.value = field.value.substring(0, max);
     else
         count.value = max - field.value.length;
 }

</SCRIPT>

Demo - Credit to www.reconn.us

Hope this helps.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
2

With Javascript, use the KeyUp event handler, and check for the legnth-property on your text field on every event.

Example: http://jsfiddle.net/nslr/C2CNS/

Thor Jacobsen
  • 8,621
  • 2
  • 27
  • 26