I have a text area field where i need to provide information about the word count when the user enters some text in the field. Length of the field is supposed to be 500 Ch
I was having trouble with the onkeydown event and had success with onkeyup. Here's the code I came up with to count down the remaining characters (with a limit of 120)
$(function() {
var input = $('#my-input'), display = $('.char-count'), count = 0, limit = 120;
count = input.val().length
remaining = limit - count
update(remaining);
input.keyup(function(e) {
count = $(this).val().length;
remaining = limit - count;
update(remaining);
});
function update(count) {
var txt = ( Math.abs(count) === 1 ) ? count + ' Character Remaining' : count + ' Characters Remaining'
display.html(txt);
}
});