jQuery Tutorial Tutorials - jQuery .addBack() method

jQuery .addBack() method

The .addBack() method in jQuery is used to add the previous set of elements on the stack to the current set.

jQuery .addBack() method Syntax

.addBack(selector)

selector is a string containing a selector expression to filter.

jQuery .addBack() method Example

example

The 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>]`

Try now

example

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>]`
Date:2019-08-26 13:16:49 From:www.Lautturi.com author:Lautturi