.addBack()
methodThe .addBack()
method in jQuery is used to add the previous set of elements on the stack to the current set.
.addBack()
method Syntax.addBack(selector)
selector
is a string containing a selector expression to filter.
.addBack()
method ExampleThe selector $( "li.third" )
get a set containing element item 3
.
The method .nextAll()
push the set of item 4,5 onto the stack.
Then addBack
merges those two sets together,so we would get a jQuery object containing elements [<li.third>,<li>,<li>]
HTML
<ul> <li>item 1</li> <li>item 2</li> <li class="third">item 3</li> <li>item 4</li> <li>item 5</li> </ul>
jQuery
$('li.third').nextAll().addBack()
Result
[<li.third>,<li>item 4</li>,<li>item 5</li>]`
The selector $( "li.third" )
get a set A
containing element item 3
.
The first .next()
push the set B
of item 4 onto the stack.
The second .next()
push the set C
of item 5 onto the stack.
Then addBack
merges set B and C
together,so we would get a jQuery object containing elements [<li>item4,<li>item5]
HTML
<ul> <li>item 1</li> <li>item 2</li> <li class="third">item 3</li> <li>item 4</li> <li>item 5</li> </ul>
jQuery
$('li.third').next().next().addBack()
Result
[<li>item 4</li>,<li>item 5</li>]`