Simple Textarea Word Counter jQuery Plugin

This is a good blog for count word from Jquery and explain in a very simple model. Blogger has shown his code in clear section that will help users in understanding. Blogger also gives code link for download.

(function($){
    $.fn.textareaCounter = function(options) {
        // setting the defaults
        // $("textarea").textareaCounter({ limit: 100 });
        var defaults = {
            limit: 100
        };
        var options = $.extend(defaults, options);
    
    // and the plugin begins
    return this.each(function() {
        var obj, text, wordcount, limited;
        
        obj = $(this);
        obj.after('
            Max. '+options.limit+' words
        ');
        
            obj.keyup(function() {
                text = obj.val();
                if(text === "") {
                    wordcount = 0;
                } else {
                    wordcount = $.trim(text).split(" ").length;
                }
                if(wordcount > options.limit) {
                    $("#counter-text").html('0 words left');
                    limited = $.trim(text).split(" ", options.limit);
                    limited = limited.join(" ");
                    $(this).val(limited);
                } else {
                    $("#counter-text").html((options.limit - wordcount)+' words left');
                }
            });
        });
    };
})(jQuery);

We got this blog from bavotasan.com for blog more information Click Here

Leave a Reply