← Back to Index

.prev()

Get the immediately preceding sibling of each element in the set of matched elements.

Examples

1. Get previous sibling

// <label>Name</label><input class="current">
$('.current').prev().addClass('bold');

Result

<label class="bold">Name</label>
<input class="current">

2. Filter previous

$('li').prev('.header').addClass('highlight');

Result

// Adds 'highlight' to the previous sibling
// ONLY IF it matches '.header'.

Parameters

Parameter Type Description
selector string Optional. If specified, retrieves the previous sibling only if it matches this selector.

Returns: A new SR object.