jQuery prevAll() Method
The jQuery prevAll()
method finds all the siblings of an element that appear before it in the HTML structure and returns them in a jQuery object.
Syntax
$(selector).prevAll(filter)
selector
- Required. The selector that identifies the element whose previous siblings to select.filter
- Optional. A selector that can be used to filter the selected siblings.
Use
The prevAll()
method is useful for selecting all the previous siblings of an element that match a given selector. This can be helpful when you need to operate on a set of elements that appear before the current element in the DOM hierarchy.
Example
Consider the following HTML structure:
<ul>
<li>Item 1</li>
<li class="selected">Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
If we want to select all the previous siblings of the currently selected item using the prevAll()
method, we can use the following jQuery code:
$("li.selected").prevAll().css("color", "red");