The end() method is useful primarily when using jQuery chain properties. If you are not using a chain, we can usually just call the previous object with the variable name, so we do not need to manipulate the stack.
A new set of items is pushed onto the stack, which is supported inside the object . Each subsequent filtering method pushes a new set of items onto the stack. If we need an older set of elements, we can use end() to pull the sets back from the stack.
I suggest using .end() as follows:
$("#test").siblings('p').remove().end().remove();
You can find the script
Some explanation:
Now what happens to this code snippet above:
Suppose this HTML markup:
<div id='container'> <div id='test'>This is supposed to be removed.</div> <p>And this to be removed too.</p> </div>
when the script executes:
$("#test").siblings('p').remove()
this block removes the sibling <p> element from the view, then the markup will be updated as follows:
<div id='container'> <div id='test'>This is supposed to be removed.</div> </div>
and if we bind it to .end()
$("#test").siblings('p').remove().end()
it returns to the stack in the #test div, after which the next part of the script runs
$("#test").siblings('p').remove().end().remove(); //--^^^^^^^^^^^^^^^----this next part
and #test are removed from the view, and your final markup output will look like this:
<div id='container'> </div>
Jai Apr 15 '13 at 6:41 2013-04-15 06:41
source share