jQuery API

.andSelf()

.andSelf() Returns: jQuery

Description: Add the previous set of elements on the stack to the current set.

  • version added: 1.2.andSelf()

As described in the discussion for .end(), jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, .andSelf() can help.

Consider a page with a simple list on it:

<ul>
   <li>list item 1</li>
   <li>list item 2</li>
   <li class="third-item">list item 3</li>
   <li>list item 4</li>
   <li>list item 5</li>
</ul>

The result of the following code is a red background behind items 3, 4 and 5:

$('li.third-item').nextAll().andSelf()
  .css('background-color', 'red');

First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to .nextAll() then pushes the set of items 4 and 5 onto the stack. Finally, the .andSelf() invocation merges these two sets together, creating a jQuery object that points to all three items in document order: {[<li.third-item>,<li>,<li> ]}.

Example:

Find all divs, and all the paragraphs inside of them, and give them both class names. Notice the div doesn't have the yellow background color since it didn't use .andSelf().

<!DOCTYPE html>
<html>
<head>
  <style>
  p, div { margin:5px; padding:5px; }
  .border { border: 2px solid red; }
  .background { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>
    <p>First Paragraph</p>
    <p>Second Paragraph</p>
  </div>
<script>
    $("div").find("p").andSelf().addClass("border");
    $("div").find("p").addClass("background");

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .andSelf() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .andSelf()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://eamon.nerbonne.org/ Eamon Nerbonne

    Does andSelf add the previous set before or after the current set?

    • Mkaestner

      As I see it is appended. Which is for me a problem.

    • http://www.learningjquery.com/ Karl Swedberg

      It keeps the resulting elements in the order in which they appear in the document.

  • http://www.google.com/profiles/nrcraver Nick Craver

    What are the chances of giving this the ability to take a selector? When any descendant or current elements that match the current selector, there’s not a quick way to do this, currently it’s .find(selector).andSelf().filter(selector) or some combination, would be nice if .andSelf() was able to take a selector already, doing the filter internally.

    • Scott Buchanan

      This would be very helpful. It would also improve performance when you need to do this, as the filter would not be run twice on the elements returned from find().

  • http://bobkerns.typepad.com/ Bob kerns

    The documentation fails to state the effect of andSelf() on the stack!

    Yes, it merges. But that leaves us three possible stack outcomes.

    A, B, A+B
    A, A+B
    A+B

    Which case is it? The documentation should state.

    • http://www.learningjquery.com/ Karl Swedberg

      Not sure what your A and B are referring to, but the last paragraph before the Example above states that the elements are returned in the order in which they appear in the document. Each element appears in the resulting set only once, so if you did $('li').filter(':first').andSelf(), the first list item would still appear once in the set, followed by the other list items.

      • http://twitter.com/vectorjohn John Reeves

        Maybe because I have the same question, I can understand what he meant.

        What is the effect of $something.andSelf().andSelf()

        His A and B refer to the stack of jQuery result sets. In the first example, he did a query that resulted in A, then one based on that which resulted in B, then he did andSelf(), which resulted in A+B being on the stack. The other examples explain themselves.

        I just did a little test which showed that andSelf() actually pushes the current result set on the stack, which is not very useful. So the answer is andSelf causes the first example, “A, B, A+B”. andSelf().andSelf() causes “A, B, A+B, A+B”

  • http://twitter.com/vectorjohn John Reeves

    Maybe because I have the same question, I can understand what he meant.

    What is the effect of $something.andSelf().andSelf()

    His A and B refer to the stack of jQuery result sets. In the first example, he did a query that resulted in A, then one based on that which resulted in B, then he did andSelf(), which resulted in A+B being on the stack. The other examples explain themselves.

    I just did a little test which showed that andSelf() actually pushes the current result set on the stack, which is not very useful. So the answer is andSelf causes the first example, “A, B, A+B”. andSelf().andSelf() causes “A, B, A+B, A+B”

  • Guest

    Can't get it :(