I want to count characters in a textarea, so I just made:
<textarea id="field" onkeyup="countChar(this)"></textarea>
function countChar(val){
var len = val.value.length;
if (len >= 500) {
val.value = val.value.substring(0, 500);
} else {
$('#charNum').text(500 - len);
}
};
What's wrong with my piece of code? It does not work! Well, that was a newbie handwriting, need a help.
U can use :
I did a combination of the above. It allows for the halting of the text entry, and allows for the backspacing, plus keeps the count, even when backspacing:
JavaScript code:
HTML:
Credit to those posters before me!! Hope this helps someone!
Try this one.
Also, you can put your own element id or class instead of "h1" and length event count your characters of text area string ☻
What errors are you seeing in the browser? I can understand why your code doesn't work if what you posted was incomplete, but without knowing that I can't know for sure.
... works fine for me.
Edit: You should probably clear the charNum div, or write something, if they are over the limit.
Your code was a bit mixed up. Here is a clean version: