Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created November 29, 2012 14:23
Show Gist options
  • Save Sigmus/4169419 to your computer and use it in GitHub Desktop.
Save Sigmus/4169419 to your computer and use it in GitHub Desktop.
Chars counter
$(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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment