.prevAll() (since jQuery 1.2) gets all preceding siblings of each matched element, optionally filtered by a selector — nearest previous sibling first.
Remember
// All previous siblings of item 3 → items 2, then 1
$("li.third-item").prevAll();
// Only previous siblings that match
$("li.third-item").prevAll(".mark");
// Document order before wrapAll (jQuery 3.7+)
$(".last").prevAll().uniqueSort().wrapAll("");
Start from an element, then collect every sibling before it.
Select the starting element(s).
Call .prevAll() for every previous sibling.
Optional: .prevAll("selector") to keep matching ones only.
Remember order: nearest first.
For wrap/append in document order, chain .uniqueSort() first.
Signature
Since
Role
.prevAll()
1.2
All preceding siblings (nearest first)
.prevAll(selector)
1.2
Same, filtered by selector
No previous siblings
—
Empty jQuery object
Pseudocode
Pseudocode
result = empty jQuery object
for each matched element:
sibling = immediate previous element sibling
while sibling exists:
if no selector OR sibling matches selector:
append sibling to result // nearest first
sibling = previous element sibling of sibling
return result
// often: .uniqueSort() before wrapAll / append
Reference
Cheat Sheet
Keep these facts handy while collecting previous siblings.
Topic
Detail
Direction
All siblings before the start
Order
Nearest first (reverse document order)
Selector
Keeps matching previous siblings only
vs .prev()
One neighbor vs all previous
vs .nextAll()
Mirror — all following siblings
wrapAll tip
Use .uniqueSort() first (3.7+)
Nearest match
.prevAll(".x").first()
Try it
Live Preview
From item 3, collect previous siblings — plain, filtered, or sorted.
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 All Previous List Items
Official idea: from item 3, select every preceding sibling.
jQuery
$("li.third-item").prevAll().prepend("[prev] ");
// items 1 and 2 both get the marker
Unlike .prev(). Filtering can find matches further back.
Nearest match. Use .prevAll(".mark").first().
Example 3 — See Nearest-First Order
Join text to show collection order: closest previous sibling first.
jQuery
var order = $("li.third-item").prevAll().map(function () {
return $(this).text();
}).get().join(" → ");
$("#out").text(order);
// list item 2 → list item 1
One neighbor versus every previous sibling 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.
prevAll. Every sibling before the start.
Choose. One step → .prev(); all → .prevAll().
Forward. Use .nextAll() for the other direction.
Edge Cases & Pitfalls
These trip up beginners most often with .prevAll().
order
Nearest-first surprises wrapAll
Collection order is reverse document order. Call .uniqueSort() before .wrapAll() when document order matters.
filter
.prev(sel) ≠ nearest match
To find the closest previous .x, use .prevAll(".x").first(), not .prev(".x").
empty
First child → empty set
No previous siblings means .length === 0. Chaining stays safe.
scope
Same parent only
Siblings share a parent. Cousins in another list are not included.
Compatibility
Browser Support
.prevAll() has been part of jQuery since 1.2. Instance .uniqueSort() needs 3.7+ (older builds: $.uniqueSort()). Works wherever your loaded jQuery build runs.
✓ Stable · jQuery 1.2+
jQuery .prevAll()
Universal across browsers that run your jQuery version. Prefer jQuery 3.7+ when using .uniqueSort() after .prevAll().
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
.prevAll()Universal
Bottom line: Safe for everyday sibling collection. Remember reverse order before wrap/append.
Analysis
Performance Notes
Approach
Cost
Best for
.prev()
Cheapest
One adjacent sibling
.prevAll() short lists
Cheap
Typical UI lists / forms
.prevAll() huge sibling sets
Watch
Prefer .prevUntil() or a tighter selector
Cost scales with how many previous siblings exist. Prefer .prev() when one neighbor is enough.
Remember
Key Takeaways
Role: collect every preceding sibling (optional filter).
Order: nearest first — sort with .uniqueSort() when document order matters.
vs prev: one step → .prev(); all → .prevAll().
Nearest match:.prevAll(".x").first(), not .prev(".x").
One line:.prevAll() gathers every previous sibling, nearest first — sort before wrap/append if you need document order.
Frequently Asked Questions
.prevAll() returns all preceding siblings of each matched element, optionally filtered by a selector. Results are in reverse document order — nearest previous sibling first. Available since jQuery 1.2.
.prev() returns at most one sibling — the immediate previous neighbor. .prevAll() returns every preceding sibling. From list item 3, .prev() gives item 2; .prevAll() gives items 2 then 1.
Official docs return preceding siblings beginning with the closest sibling. So from item 3 you get [2, 1], not [1, 2]. That matters for APIs like .wrapAll() that process nodes in collection order.
Before methods that append or wrap in collection order (e.g. .wrapAll()). Call .uniqueSort() (jQuery 3.7+) or $.uniqueSort() on older builds so nodes are in document order.
You get an empty jQuery object (length 0). Chaining is safe.
Use .prevAll(".selected").first() — .prev(".selected") only tests the immediate neighbor and will not walk further.