jQuery API

.next()

.next( [selector] ) Returns: jQuery

Description: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

  • version added: 1.0.next( [selector] )

    selectorA string containing a selector expression to match elements against.

Given a jQuery object that represents a set of DOM elements, the .next() method allows us to search through the immediately following sibling of these elements in the DOM tree and construct a new jQuery object from the matching elements.

The method optionally accepts a selector expression of the same type that we can pass to the $() function. If the immediately following sibling matches the selector, it remains in the newly constructed jQuery object; otherwise, it is excluded.

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>

If we begin at the third item, we can find the element which comes just after it:

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

The result of this call is a red background behind item 4. Since we do not supply a selector expression, this following element is unequivocally included as part of the object. If we had supplied one, the element would be tested for a match before it was included.

Examples:

Example: Find the very next sibling of each disabled button and change its text "this button is disabled".

<!DOCTYPE html>
<html>
<head>
  <style>

  span { color:blue; font-weight:bold; }
  button { width:100px; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div><button disabled="disabled">First</button> - <span></span></div>
  <div><button>Second</button> - <span></span></div>

  <div><button disabled="disabled">Third</button> - <span></span></div>
<script>$("button[disabled]").next().text("this button is disabled");</script>

</body>
</html>

Demo:

Example: Find the very next sibling of each paragraph. Keep only the ones with a class "selected".

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p>

  <p class="selected">Hello Again</p>
  <div><span>And Again</span></div>
<script>$("p").next(".selected").css("background", "yellow");</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .next() 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 .next()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Szczepan2

    What is returned if there is no next item?

    Please document such details for EVERY function.

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

    Click on the “Returns: jQuery” link from ANY of these methods now to find out what is returned if there are no elements in the matched set.

  • eriz

    AFAIR, jQuery object but .length property is 0 (zero).

    “Please document such details for EVERY function.”

    All traversing functions behave in such a way.

  • http://www.mordraug.ru/ Egor Kashlinov

    why next() with class selector expression doesn't work if target sibling is not exactly next?

    For example:
    We have 5 paragraphs with classes “first”, “second”, “third”, “fourth” and “fifth”.
    And now if we try this: $(“p.first”).next(“.fourth”).css(“color”,”red”);

    we'll see that it doesn't work.
    But this: $(“p.first”).next(“.second”).css(“color”,”red”);
    will work right.

  • http://www.irishwebhq.com/ Mark O'Leary

    Try using nextAll() instead of next(). Should reference all siblings rather than just the next one.

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

    I would like to throw this out there: based on the number of questions/confusion I see over this, a .next(selector, true) override for this would be very helpful, equivalent to .nextAll(selector:first), which is far less discoverable/intuitive.

    This is based on the number of StackOverflow questions in the past few months around .next() and confusion when it doesn't return the “next element matching the selector”, instead of the “very next element IF it matches the selector”. Maybe it's just me, but it feels backwards…I understand a breaking change is out of the question, but a boolean overload would give a more natural option at least.

  • http://www.martinsmucker.com Michael Martin-Smucker

    Thank you! I would have been confused for much longer if I hadn't read this first. I was trying to make a certain class of links always open the first following hidden paragraph, and I couldn't figure out why it wasn't working – turns out there was another sibling link in between the first link and the hidden paragraph. This worked like a charm:

    $(“a.description”).click(function(){
    $(this).nextAll(“p.hidden:first”).slideToggle();
    });

  • http://qube.carbonmade.com adir

    same here.
    so happy i found this after just 20 mins and not 3 days.

    it should be “next element which matches selector”

    thanks Nick

  • Nikki Locke

    “If a selector is provided, it retrieves the next sibling that matches the selector.” is misleading. If this was reworded to say
    “If a selector is provided, it retrieves the next sibling only if it matches the selector.”
    it might be clearer.
    (Presumably prev should also be reworded).

  • Francisc

    Hey

  • Francisc

    Hello!

    There's a typo here: “If the the immediately following sibling matches the selector, it remains in the newly constructed jQuery object; otherwise, it is excluded.”.

    “the the” is repeated.

  • gwideman

    Is there any way to use next() and prev() to select the next or prev elements within a provided wrapped set instead of the main DOM? That means there would be two inputs: A wrapped set containing the current universe of items, and a second wrapped set telling which items to find the next or prev siblings of.
    Thanks,
    Graham

  • gwideman

    Is there any way to use next() and prev() to select the next or prev elements within a provided wrapped set instead of the main DOM? That means there would be two inputs: A wrapped set containing the current universe of items, and a second wrapped set telling which items to find the next or prev siblings of.
    Thanks,
    Graham

  • No World Order

    Does next() loop to the first sibling if it's reach the end of the list? For example: say you have four divs, when you use next() on the last div will next() then return the first div?

  • Brian

    I'm running into this issue now. No, next() does not. I'm trying to figure out how to check if next() is invalid, so that I loop back to the beginning as well!

  • digitalcanopy

    Solved it thanks to @eriz below. Simply test the length of the element returned by next(). If it equals zero, go back to the first element.

    Example:

    if($(this).parent().next().length == 0) {

    $('#news ul li:first-child').addClass('active');
    }
    else {
    $(this).parent().next().addClass('active');
    }

    - Brian