Definition and Usage

nextUntil() returns all sibling elements between two given parameters.

Note: This method returns the elements between the two given parameters, but does not include those two elements.

Related methods:

  • next() - returns the next sibling element of the selected element
  • nextAll() - returns all sibling elements after the selected element

Grammar

$(selector).nextUntil(filter, filter2)
Parameter Description
filter Optional. Specifies the parameter that stops the search for matching sibling elements.

The parameter can be any selector (such as a tag name, class name, id, etc.), a DOM element, or a jQuery object.

Note: To return all sibling elements after the selected element, use the nextAll() method.

filter2 Optional. A selector expression used to narrow down the search for sibling elements between the first parameter and the element matched by this parameter.

Example 1

Return all sibling elements between <h2> and <h6>:

$("h2").nextUntil("h6")

Example 2

Use DOM elements as parameters:

var limit = document.getElementById("stop");
$("li.start").nextUntil(limit, ".first");

Example

Return all sibling elements between two given elements:

Example

$(document).ready(function(){
    $("li.start").nextUntil("li.stop").css({"color":"red","border":"2px solid red"});
});

Try it Yourself »

Example

Add a class name to each element between two given parameters:

Example

$(document).ready(function(){
    $("li.start").nextUntil("li.stop").addClass("between");
});

Try it Yourself »

Example

Use both parameters to narrow down the search:

Example

$(document).ready(function(){
    $("h2").nextUntil("h6", "h4").css({"color":"red","border":"2px solid red"});
});

Try it Yourself »