Skip to content

Instantly share code, notes, and snippets.

@MoritzBuetzer
Created July 10, 2014 11:52
Show Gist options
  • Save MoritzBuetzer/a79b68e16d7fa69d969c to your computer and use it in GitHub Desktop.
Save MoritzBuetzer/a79b68e16d7fa69d969c to your computer and use it in GitHub Desktop.
simple script to limit chars in textfield
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);
// $("textfield").limiter(100, "#remainingCharsElement");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment