How to count characters entered into a textarea using JavaScript

Home > Search > How-to
  by

Added the following script to the HEAD of your markup.

<script src="http://code.jquery.com/jquery-1.5.js"></script>
<script>
function CountCharacters(val) {
  var len = val.value.length;
  if (len >= 50) {val.value = val.value.substring(0, 50); }
  else {$('#charNum').text(50 - len); }
};
</script>

 

Then, add onkeyup="CountCharacters(this)" to the textarea.

<textarea onkeyup="CountCharacters(this)"></textarea>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments