Basic Textarea Remaining Characters Script


/ Published in: JavaScript
Save to your folder(s)

different solution by PPK:\\r\\n[http://www.quirksmode.org/book/examplescripts/maxlength/index.html](http://www.quirksmode.org/book/examplescripts/maxlength/index.html)


Copy this code and paste it in your HTML
  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>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.