Here is the definition of the StackExchange charCounter () method. This is a bit confusing, but you can find the logic if you dig it:
charCounter: function(c) { return this.each(function() { var d = $(this).parents("form").find("span.text-counter"); var e = this; var f = function() { var j = c.min; var l = c.max; var k = c.setIsValid || function() {}; var h = e.value ? e.value.length : 0; var i = h > l * .8 ? "supernova" : h > l * .6 ? "hot" : h > l * .4 ? "warm" : "cool"; var g = ""; if (h == 0) { g = "enter at least " + j + " characters"; k(false); } else { if (h < j) { g = j - h + " more to go.."; k(false); } else { g = l - h + " character" + (l - h != 1 ? "s" : "") + " left"; k(h <= l); } } d.text(g); if (!d.hasClass(i)) { d.removeClass("supernova hot warm cool").addClass(i); } }; $(this).bind("blur focus keyup", a.DelayedReaction(f, 100, { sliding: true }).trigger); }); }
And comment text areas, for example, are set up like this (again, obfuscation):
var x = z.find("textarea"); x.charCounter({ min: 15, max: 600, setIsValid: A });
source share