Skip to content

Instantly share code, notes, and snippets.

@sharmaansh21
Created January 5, 2014 06:59
Show Gist options
  • Select an option

  • Save sharmaansh21/8265272 to your computer and use it in GitHub Desktop.

Select an option

Save sharmaansh21/8265272 to your computer and use it in GitHub Desktop.
limiter
(function($) {
$.fn.extend( {
limiter: function(limit, elem) {
$(this).on("keyup focus", function() {
setCount(this, elem);
});
function setCount(src, elem) {
var chars = src.value.length;
if (chars > limit) {
src.value = src.value.substr(0, limit);
chars = limit;
}
elem.html( limit - chars );
}
setCount($(this)[0], elem);
}
});
})(jQuery);
To setup the limiter, simply include a call similar to the one below:
var elem = $("#chars");
$("#text").limiter(100, elem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment