2011-12-18 14:46:16 +00:00
|
|
|
/**
|
|
|
|
* jCounter - a simple character counter
|
|
|
|
*
|
|
|
|
* @author Maximilian Mader
|
|
|
|
* @copyright 2011 Maximilian Mader
|
|
|
|
* @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
|
|
|
|
* @package jQuery.jCounter
|
|
|
|
*/
|
2011-12-17 20:29:57 +00:00
|
|
|
(function($){
|
2011-12-18 14:46:16 +00:00
|
|
|
$.fn.jCounter = function(max, options) {
|
|
|
|
maxChars = max || 140;
|
|
|
|
options = $.extend({
|
|
|
|
container: '<span></span>',
|
|
|
|
counterClass: 'counter',
|
|
|
|
countUp: false
|
|
|
|
}, options);
|
|
|
|
var timeout;
|
|
|
|
|
|
|
|
jCounterContainer = $(options.container);
|
|
|
|
|
2011-12-17 20:29:57 +00:00
|
|
|
this.on('keypress keydown keyup', $.proxy(function() {
|
2011-12-18 14:46:16 +00:00
|
|
|
if(options.countUp) length = this.val().length;
|
|
|
|
else length = maxChars - this.val().length;
|
|
|
|
|
|
|
|
if(options.countUp) color = 1;
|
|
|
|
else {
|
|
|
|
if (length > maxChars / 2) color = 1;
|
|
|
|
else if (length <= maxChars / 2 && length >= maxChars / 6) color = 2;
|
|
|
|
else color = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
jCounterContainer.text(length).attr('class', '').addClass(options.counterClass + ' color-'+color);
|
2011-12-17 20:29:57 +00:00
|
|
|
}, this));
|
|
|
|
}
|
|
|
|
})(jQuery);
|