jQuery Traversal - nextUntil() Method
Example
Find the following sibling elements following <dt id="term-2"> until the next <dt>, and set their background color to red. At the same time, find the <dd> following sibling elements following <dt id="term-1"> until <dt id="term-3">, and set their text color to blue.
$("#term-2").nextUntil("dt").css("background-color", "red"); var term3 = document.getElementById("term-3"); $("#term-1").nextUntil(term3, "dd").css("color", "blue");
Definition and Usage
The nextUntil() method of jQuery gets all following sibling elements of each element, but does not include elements matched by selector, DOM node, or the jQuery object passed.
Syntax 1
.nextUntil(selector,filter)
Parameter | Description |
---|---|
selector | String value, containing the selector expression indicating where to stop matching following sibling elements. |
filter | String value, containing the selector expression used to match elements. |
Syntax 2
.nextUntil(element,filter)
Parameter | Description |
---|---|
element | Indicates the DOM node or jQuery object where the matching of following sibling elements should stop. |
filter | String value, containing the selector expression used to match elements. |
Detailed Description
If a jQuery object representing a set of DOM elements is given, the .nextUntil() method allows us to search for the following sibling elements of elements in the DOM tree, stopping the search when an element matching the parameter of this method is encountered. The new jQuery object returned contains all following sibling elements but does not contain the element matched by the parameter.
If the selector does not match or no selector is specified, it will select all following siblings; if no selector for filtering is provided, the elements selected by this method are the same as those selected by the .nextAll() method.
For jQuery 1.6, DOM nodes or jQuery objects, rather than selectors, can be passed to the .nextUntil() method.
This method accepts an optional selector expression as its second parameter. If this parameter is specified, it will filter them by checking if the elements match the selector.