Posted By

cfleschhut on 08/27/10


Tagged

forms limit textarea maxlength


Versions (?)

Basic Textarea Remaining Characters Script


 / Published in: JavaScript
 

URL: http://jsfiddle.net/BdRn2/

  1. function textCounter(field, countfield, maxlimit) {
  2. if (field.value.length > maxlimit) {
  3. field.value = field.value.substring(0, maxlimit);
  4. } else {
  5. countfield.value = maxlimit - field.value.length;
  6. }
  7. }
  8.  
  9. window.onload = function() {
  10. var ta = document.getElementById("message"),
  11. inp = document.getElementById("remLen"),
  12. limit = 50;
  13. inp.value = limit;
  14. ta.onkeyup = function() {
  15. textCounter(this, inp, limit);
  16. }
  17. }
  18.  
  19.  
  20. <form>
  21. <p>
  22. <textarea id="message" cols="40" rows="8"></textarea>
  23. </p>
  24. <p>
  25. <label><input type="text" id="remLen" size="3" /> remaining characters</label>
  26. </p>
  27. </form>

Report this snippet  

You need to login to post a comment.