//fn is the jquery prototype object //chainability of your plugin can be maintained by returning this, this.each or this.pushStack (function($) { $.fn.changeBackColor= function(options) { var settings = {}; $.extend(settings, this.changeBackColor.defaults, options); return this.css('background-color', settings.backColor); }; // defining the defaults for options $.fn.changeBackColor.defaults = { backColor: 'yellow' }; // a plugin that returns a value $.fn.average = function() { var sum = 0; this.each(function() { sum += parseFloat($(this).html()); }); return sum / $(this).length; }; })(jQuery); $(function() { $.fn.changeBackColor.defaults.backColor = 'red'; $('#div1').changeBackColor(); $('#div2').changeBackColor({ backColor: 'green' }); alert("Average is : " + $('div').average()); });
<div id="div1">1</div> <div id="div2">2</div> <div id="div3">3</div> <div id="div4">4</div>