Tutorial :jQuery : return this


In jQuery plugins, every time we return an object of jQuery, like

$.fn.Myplugin = function() {        return this.each(function() { //do some stuff });  });  

My doubt is, why do we actually return an object of jQuery and where are we going to use this returned object

Though I don't return any jQuery object in my function(plugin), I still get the same result just as in returning an object

someone please explain my doubt,

Thanks for sparing your time, Have a Good Day !


The jQuery object is returned so we can do method chaining:


If you don't return it from one of your plugin methods, there's no more jQuery object to work with so subsequent calls will result in an error.


All jQuery methods return you jQuery Object so that the set of results returned can further be used to perform other operations this is also called chaining. This is done for tersness in the code. Its easy to code (atleast for me) and prevents me writing big loops.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »