jQuery prevUntil()
The jQuery prevUntil()
function is used to select all the preceding siblings of an element until another element is encountered.
Syntax
The syntax for prevUntil()
function is as follows:
$(selector).prevUntil(selector, filter);
selector
: Specifies the element(s) to stop selection. The selection will stop at this point. Optional.filter
: Specifies a filter function to display specific elements. Optional.
Use
The prevUntil()
method is useful when you need to select a range of elements preceding an element. For example, you might want to select all the table cells above a specific checkbox, but you don't know exactly how many cells there are. prevUntil()
can be used to specify the stopping point for the selection.
Example
Consider the following HTML code:
<div class="container">
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p class="selected">Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
</div>
The following jQuery code will select all the p
elements preceding .selected
until the first p
element is encountered:
$(".selected").prevUntil("p:first").css("color", "red");