jQuery Tutorial Tutorials - jQuery .andSelf() method

jQuery .andSelf() method

The .andSelf() method in jQuery is used to add the previous set of elements on the stack to the current set.But it has been deprecated and please use .addBack() instead.

jQuery .andSelf() method Syntax

.andSelf()

jQuery .andSelf() 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 andSelf 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().andSelf()

Result

[<li.third>,<li>item 4</li>,<li>item 5</li>]`
Date:2019-08-26 13:18:12 From:www.Lautturi.com author:Lautturi