Skip to content

Instantly share code, notes, and snippets.

@sblmasta
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sblmasta/8865459 to your computer and use it in GitHub Desktop.
Save sblmasta/8865459 to your computer and use it in GitHub Desktop.
$.fn.extend({
limitText: function(config){
$(this).after('<div id="counter">Limit znaków: <span></span></div>');
var limit = typeof(config.limit) == 'undefined' ? this.attr('maxlength') : config.limit;
var counter_hook = typeof(config.counter) == 'undefined' ? '#counter' : config.counter;
$(counter_hook).data('value', limit);
$(counter_hook).find('span').html(limit);
$(this).on('keyup keydown', function(){
if( $(this).length > limit )
{
this.value = this.value.substring(0, limit);
}
else
{
$(counter_hook).find('span').html(limit - $(this).val().length);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment