Created
December 9, 2011 08:03
-
-
Save roundrop/1450687 to your computer and use it in GitHub Desktop.
jQuery plugin sample: Twitter-like dynamic character countdown for textareas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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