Return to Snippet

Revision: 40346
at January 31, 2011 09:11 by edwardhotchkiss


Initial Code
(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);

Initial URL

                                

Initial Description

                                

Initial Title
Twitter like Character Counter

Initial Tags
javascript, jquery, twitter

Initial Language
jQuery