jQuery Traversal - prevUntil() Method
Example
Find the preceding sibling elements before <dt id="term-2"> until the previous <dt>, and set them to red. At the same time, find the preceding <dd> sibling elements before <dt id="term-3"> until <dt id="term-1">, and set their text color to blue:
$("#term-2").prevUntil("dt").css("background-color", "red"); var term1 = document.getElementById('term-1'); $("#term-3").prevUntil(term1, "dd").css("color", "green");
Definition and Usage
The prevUntil() method gets the preceding sibling elements of each element in the current matched element set, but does not include elements matched by the selector, DOM node, or jQuery object.
Syntax 1
.prevUntil(selector, filter)
Parameter | Description |
---|---|
selector | Optional. A string value containing the selector expression indicating where to stop matching preceding sibling elements. |
filter | Optional. A string value containing the selector expression used to match elements. |
Syntax 2
.prevUntil(element, filter)
Parameter | Description |
---|---|
element | Optional. Indicates the DOM node or jQuery object at which to stop matching preceding sibling elements. |
filter | Optional. A 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 .prevUntil() method allows us to search for preceding sibling elements of these elements in the DOM tree until we encounter an element that matches the selector (passed as a parameter to the method). The returned jQuery object contains all preceding sibling elements but does not include the element matched by the .prevUntil() method; the order of the returned elements is from the nearest sibling to the farthest one.
If no match is found or the selector is not applied, all preceding sibling elements in the selection will be selected; in this case, the elements selected by this method are the same as those selected by .prevAll() without a selector provided.
For jQuery 1.6, a DOM node or a jQuery object, rather than a selector, can be used as the first parameter of the .prevUntil() method.
This method accepts an optional selector expression as its second parameter. If this parameter is applied, elements will be filtered by checking whether they match the selector.