Wednesday, September 4, 2013

Counting number of characters typed on textarea using jQuery



There is textarea for input with id mytext
<textarea id="mytext" ></textarea>
A  div with id char_counter for counting number of characters typed in textarea
<div id="char_counter"></div>

jQuery code: using keyup to update the characters typed.

$(document).ready(function(){
 
    $('#mytext').keyup( function(){
   var text_length = $('#mytext').val().length;

    $('#char_counter').html(text_length + ' characters  are typed');
  });

});
 

if space is not counted, we should use 
var text_length = $.trim($('#mytext').val()).length;

No comments:

Post a Comment