Possible Duplicate:
How does the jQuery pushStack function work?
In the jQuery API I saw a function pushStack. The description sais: Add a collection of DOM elements onto the jQuery stack.
Does anyone know wat the jQuery stack is and for what can it be used? Does it has a relation with the DOM?
When you chain multiple jQuery methods together and each method returns a new jQuery object, jQuery keeps track of all the jQuery objects in a stack. This allows you to return to a previously used jQuery object without having to save it in a local variable. To make this work, when a jQuery method creates a new jQuery object as a result of a method call, it calls
pushStack()to allow the new object to participate in the stack.The jQuery method
.end()is somewhat the reverse of.pushStack()in that it goes one item back in the stack to get the prior jQuery object and it can be called multiple times to continue going back in the stack. See the.end()doc for more info.For an example of using
.pushStack(), let’s say you wanted a method that would get all text nodes in a container, you could do so like this and return the new resulting jQuery object using.pushStack():