/ Published in: jQuery
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
(function($) { $.fn.CharacterCounter = function(options) { var defaults = { allowed : 140, counterText : "Characters left: ", overlimitClass : "overlimit_txt", pID : "#character_count", txtColor : "#ff0000" }; var options = $.extend(defaults, options); function calculate(obj) { var count = $(obj).val().length; var available = options.allowed - count; if (available == 0) { $(options.pID).addClass(options.overlimitClass); } else if (available > 0) { $(options.pID).removeClass(options.overlimitClass); } $(options.pID).html(options.counterText + available); } calculate(this); $(this).keyup(function() { calculate(this) }); $(this).change(function(){ calculate(this) }); }; })(jQuery);