in jQuery

Simple jQuery Plugin Example

3. Working on the chain gang …

Method chaining allows you to write more terse code by making the return value of each method the object on which the method is operating. This is the name for what is happening when you see something like this:

$(".hidden").fadeIn("slow").css("color", "#FFF");

Preserving method chaining is very easy to achieve in your plugin by doing the following:

(function($) {
    $.fn.explainify = function() {
        /**
         * Return the object to preserve method chaining
         */
        return this.each(function(i) {
            // Do some kick-ass stuff here
        });
    };
})(jQuery);

We are returning the element – stored in the this object – at the completion of each iteration.