Skip to content

Instantly share code, notes, and snippets.

@roundrop
Created December 9, 2011 08:03
Show Gist options
  • Save roundrop/1450687 to your computer and use it in GitHub Desktop.
Save roundrop/1450687 to your computer and use it in GitHub Desktop.
jQuery plugin sample: Twitter-like dynamic character countdown for textareas
/*
jQuery plugin sample: Twitter-like dynamic character countdown for textareas
Example:
$('#textarea').countdown({
limit: 140,
init: function(counter){
$('#counter').css('color','#999999').val(counter);
$('#submit').attr('disabled','disabled');
},
plus: function(counter){
$('#counter').css('color','#999999').val(counter);
$('#submit').removeAttr('disabled');
},
minus: function(counter){
$('#counter').css('color','red').val(counter);
$('#submit').attr('disabled','disabled');
}
});
*/
(function($) {
$.fn.countdown = function(config) {
var options = $.extend($.fn.countdown.defaults, config);
return this.each(function() {
$(this).bind('keyup change', function() {
if(!$(this).val().length){
options.init(options.limit);
return;
}
var available = options.limit - $(this).val().length;
var counter = options.prefix + available + options.suffix;
if (counter >= 0)
options.plus(counter);
else
options.minus(counter);
});
});
};
$.fn.countdown.defaults = {
limit: 140,
init: function(counter){},
plus: function(counter){},
minus: function(counter){},
prefix: '',
suffix: ''
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment