text counter refactor

This commit is contained in:
Mark Moser 2016-09-01 10:49:49 -05:00
parent a1b93f256d
commit 6639f1c6e7

View File

@ -1,21 +1,22 @@
$.fn.extend({ $.fn.extend({
characterLimiter: function(limit, $label) { characterLimiter: function(limit, label) {
$(this).on("keyup focus show", function() { this.on("keyup focus show", function() {
setCount(this, $label); setCount(this, label);
}); });
// TODO: append label container after $this, instead of hard codeing // TODO: append label container after $this, instead of hard HTML
function setCount(src, $label) { function setCount(src, label) {
if(src !== undefined) { if(src !== undefined) {
var chars = src.value.length; var chars = src.value.length;
if (chars >= limit) { if (chars >= limit) {
src.value = src.value.substr(0, limit); src.value = src.value.substr(0, limit);
chars = limit; chars = limit;
} }
$label.html(limit - chars); label.html(limit - chars);
} }
} }
setCount($(this)[0], $label);
setCount(this[0], label);
} }
}); });