Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 614
UserIsCorrupt Avatar asked Feb 04 '12 16:02

UserIsCorrupt


2 Answers

Example - This will alert out the number of characters

$('#textBoxId').bind('keyup', function(e){

     alert($(this).val().length);

});

This obviously assumes that the text box has an id of textBoxId. Otherwise change selector iof don't want to give it an id for some reason

like image 115
Crab Bucket Avatar answered Oct 27 '22 00:10

Crab Bucket


$('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);
});
like image 43
gion_13 Avatar answered Oct 26 '22 23:10

gion_13