Skip to content

Instantly share code, notes, and snippets.

@octavioamu
Created September 26, 2013 21:06
Show Gist options
  • Save octavioamu/6720562 to your computer and use it in GitHub Desktop.
Save octavioamu/6720562 to your computer and use it in GitHub Desktop.
$.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);
}
});
var elem = $('#text');
$("#content").limiter(140, elem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment