.prev()
S2C Home « Traversal « .prev()
Previous sibling element retrieval.
Description
The .prev()
method is used to retrieve the immediately preceding sibling of each element within the matched set.
Syntax
Signature | Description |
---|---|
.prev() | Retrieve the immediately preceding sibling of each element within the matched set. |
.prev( selector ) | Retrieve the immediately preceding sibling of each element within the matched set, but only if it matches the specified selector. |
Parameters
Parameter | Description | Type |
---|---|---|
selector | A string containing a CSS or custom jQuery selector. | Selector |
Return
A jQuery
object either containing the elements matched or empty.
.prev()
ExampleTop
Retrieve the immediately preceding sibling of each element within the matched set.
In the example below we select all immediately preceding sibling elements of 'p' elements within the #main section of the page and turn their background colour yellow.
$(function(){
$('#btn5').on('click', function() {
$('#main p').prev()
.css('backgroundColor', 'yellow');
});
});
.prev( selector )
ExampleTop
Retrieve the immediately preceding sibling of each element within the matched set, but only if it matches the specified selector.
In the example below we select all immediately preceding 'h4' elements of 'p' elements within the #main section of the page and turn their background colour orange.
$(function(){
$('#btn6').on('click', function() {
$('#main p').prev('h4')
.css('backgroundColor', 'orange');
});
});