Skip to content

Instantly share code, notes, and snippets.

@b1ff
Created May 18, 2012 14:49
Show Gist options
  • Save b1ff/2725650 to your computer and use it in GitHub Desktop.
Save b1ff/2725650 to your computer and use it in GitHub Desktop.
$countableTextAreas.live('keypress change', (function () {
var previousText = '';
return function () {
var $this = $(this),
allowedChars = Number.parseLocale($this.attr('data-maxLength')),
currLength = $this.val().length,
$spanWithCounter = $this.next('span.symbolCounter').find('span');
if (previousText == '') {
previousText = $this.val().substring(0, allowedChars);
}
if (currLength > allowedChars) {
$spanWithCounter.stop(true, true);
$spanWithCounter.text(currLength);
$spanWithCounter.fadeOut('slow', function () {
$spanWithCounter.fadeIn('slow');
$spanWithCounter.text(previousText.length);
});
$this.val(previousText);
return false;
}
$spanWithCounter.text(currLength);
previousText = $this.val();
};
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment