Simple way to count characters using .keyup in jQuery

UserIsCorrupt picture UserIsCorrupt · Feb 4, 2012 · Viewed 39.7k times · Source
<input type="text" />

How can I write the number of characters from input on .keyup in JavaScript/jQuery?

Answer

gion_13 picture gion_13 · Feb 4, 2012
$('input').keyup(function() {
    console.log(this.value.length);
});

keyup is a shortcut method for bind('keyup').
And as of jQuery version 1.7, all of the above are deprecated we are encourage to use the on method to bind events, meaning that the code should look like this:

$('input').on('keyup', function() {
    console.log(this.value.length);
});