Search code examples
javascriptjqueryonkeyup

Simple way to count characters using .keyup in jQuery


<input type="text" />

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


Solution

  • $('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);
    });