.prev() (since jQuery 1.0) gets the immediately preceding sibling of each matched element. An optional selector includes that sibling only if it matches.
Remember
// Immediate previous sibling
$("li.third-item").prev();
// Only if that sibling matches
$("p").prev(".selected");
// No previous / no match → empty set
$("#first").prev(); // length 0
Start from an element, then take one step to its previous sibling.
Select the starting element(s).
Call .prev() for the immediate previous sibling.
Optional: .prev("selector") — keep it only if it matches.
Act on the result (.prepend(), .text(), …).
If .length is 0, there was no neighbor (or no match).
Signature
Since
Role
.prev()
1.0
Immediate previous sibling
.prev(selector)
1.0
Same, only if sibling matches
No sibling / no match
—
Empty jQuery object
Pseudocode
Pseudocode
result = empty jQuery object
for each matched element:
sibling = immediate previous element sibling
if no sibling:
skip
else if selector was given and sibling does not match:
skip
else:
add sibling to result
return result
Reference
Cheat Sheet
Keep these facts handy while walking siblings.
Topic
Detail
Direction
One sibling backward
Nodes
Element siblings only (not text/comment)
Selector
Filters that one neighbor — no further scan
Empty result
Safe to chain; check .length if needed
vs .prevAll()
All previous siblings
vs .next()
Mirror — one step forward
Returns
New jQuery object (chainable)
Try it
Live Preview
Start from item 3 and step with .prev(), .next(), or a filtered prev.
Markers use text — no CSS required.
list item 1
list item 2
list item 3
list item 4
list item 5
Result
Ready — pick an action and click Run.
Code
jQuery Examples
Short, copy-ready snippets. Prefer text markers so demos stay CSS-free.
Example 1 — Mark the Previous List Item
Official idea: from item 3, select the immediate previous sibling.
jQuery
$("li.third-item").prev().prepend("[prev] ");
// list item 2 becomes "[prev] list item 2"
One step vs all previous siblings from the same start.
jQuery
var one = $("li.third-item").prev().text();
var all = $("li.third-item").prevAll().map(function () {
return $(this).text();
}).get().join(", ");
$("#out").text("prev: " + one + " | prevAll: " + all);
// prev: list item 2 | prevAll: list item 2, list item 1
prev: list item 2 | prevAll: list item 2, list item 1
How It Works
prev. Only the immediate neighbor (item 2).
prevAll. Every sibling before the start (2, then 1).
Order..prevAll() is nearest-first.
Choose. One step → .prev(); all → .prevAll().
Edge Cases & Pitfalls
These trip up beginners most often with .prev().
filter
Selector does not walk further
.prev(".x") tests only the immediate sibling. For the nearest previous .x, use .prevAll(".x").first().
empty
First child → empty set
No previous element sibling means .length === 0. Check length before assuming a match.
nodes
Whitespace is ignored
.prev() skips text/comment nodes between tags. Only element siblings count.
parent
Not a parent or child
Sibling methods stay on the same level. Use .parent() / .children() for vertical moves.
Compatibility
Browser Support
.prev() has been part of jQuery since 1.0. Works wherever your loaded jQuery build runs.
✓ Stable · jQuery 1.0+
jQuery .prev()
Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for teaching and production.
100%jQuery core
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.prev()Universal
Bottom line: Safe for everyday sibling walking. Use .prevAll() when you need more than one step back.
Analysis
Performance Notes
Approach
Cost
Best for
.prev()
Cheap
One adjacent sibling
.prev(selector)
Cheap
One neighbor + quick match test
.prevAll() on long lists
Watch
Prefer when you truly need every previous sibling
One step is inexpensive. Prefer .prev() over .prevAll() when a single neighbor is enough.
Remember
Key Takeaways
Role: one step to the immediately preceding sibling.
Filter:.prev(selector) tests that one neighbor only.
Empty: no sibling or no match → length 0; chaining is safe.
Choose: one step → .prev() · all → .prevAll() · forward → .next().
One line:.prev() is one sibling backward — optional selector filters that neighbor, nothing further.
Frequently Asked Questions
.prev() returns the immediately preceding sibling of each element in the matched set. It moves sideways in the DOM — not to parents or children. Available since jQuery 1.0.
.prev() returns at most one sibling — the direct previous neighbor. .prevAll() returns every preceding sibling. From list item 3, .prev() gives item 2 only; .prevAll() gives items 1 and 2.
.prev() looks backward one step; .next() looks forward one step. Same distance, opposite direction.
jQuery returns an empty object (length 0). Chaining is safe — .prev().addClass("x") does nothing when there is no neighbor.
No. It tests only the immediately preceding sibling. If that sibling does not match, the result is empty. Use .prevAll(selector).first() when you need the nearest previous match further back.
No. Like other sibling methods, .prev() considers element nodes only — whitespace text between tags is ignored.