JavaScript - How to count characters entered into a textarea using JavaScript


by
Jeremy Canfield |
Updated: June 15 2024
| JavaScript articles
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>
Did you find this article helpful?
If so, consider buying me a coffee over at