fredyounan
4/15/2015 - 9:59 AM

$(function(){ charsCounter($('.texto'), $('.counter'), 400); });

$(function(){ charsCounter($('.texto'), $('.counter'), 400); });

$(function(){
  charsCounter($('.texto'), $('.counter'), 400);
});
  
function charsCounter($input, $counter, max) {
  $input.bind('keyup', update).bind('paste', update);

  function update() {
    var numChars = $input.val().length;
    var remaining = max - numChars;

    if (numChars >= max) {
      $input.val($input.val().slice(0, max - 1));  
    }
    
    if (remaining < 0) {
      remaining = 0;  
    }
    $counter.text(remaining);
  }
}