Friday, February 17, 2012

jQuery limit word plugin

This is a jquery plug in that limits the number words to display depending on how many words you want to display.

(function($){
    $.fn.limitWord= function(options){
        var defaults={
        /* by default */
            words:5
        }
        var options=$.extend(defaults,options);
        return this.each(function(){
            var paragraph=$(this).text();
            var splitSpace = paragraph.split(" ");
            var holder = options.words;
            var newTexts='';
            for(var i=0;i<holder;i++){
                newTexts += splitSpace[i]+' ';
            }
            $(this).html(newTexts); 
        });
    }
})(jQuery);

How to use:
jQuery("class or id here").limitWord({
words:5 /* please specify the number of words to be displayed */
});

No comments:

Post a Comment