Character Limiter: My first Jquery plugin

I was looking for a character limiter for jhoroTEK SMS server reporting panel. Then i found this one in ajaxray’s blog.  Though i liked the code much but writting some extra targeted code seems too much for me. So, i forked a jquery plugin from that one.

Let’s say your textarea class name is ‘test’.

in your HTML head add this :


$(document).ready(
function() {
$('.test').startCounting({limit: 160});
}
);

And here is the plugin file name:  ajaxray_textcounting.js


/*
Original Code was by: AjaxRay ( http://www.ajaxray.com/blog/2007/11/09/interactive-character-limit-for-textarea-using-jquery/ )
Plugin Written by : Mohammad Amzad Hossain ( tohin.wordpress.com)
Thanks to : http://www.learningjquery.com/2007/10/a-plugin-development-pattern
*/

(function($) {

$.fn.startCounting = function(options){

var defaults = {
limit: 30
};

options = $.extend( defaults, options);

return $(this).each( function(i) {

var elem = $(this);
elem.after('');
elem.keyup( function(i) {
var limit = options.limit;
var text = elem.val();
var textlength = text.length;

if( textlength > limit)
{
elem.next('.counting_class').val( limit + ' / ' + limit);
elem.val(text.substr(0,limit));
return false;
}
else
{
elem.next('.counting_class').val( textlength + ' / ' + limit);
return true;
}
});
elem.trigger('keyup');
});
};

})(jQuery);

I hope this comes handy… as it comes to me. Dont’ forget to add jquery as it depends on jquery.

Note: Please let me know if you have any pointing, suggestion or bugs :).

Please Download the js file from Download Section or click here . Thanks

Advertisements