jQuery .prevAll() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .prevAll()?

.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("
");
NeedUse
One step back.prev()
All previous siblings.prevAll()
Previous until a stop.prevUntil()
All following siblings.nextAll()
Either side.siblings()

How to Use .prevAll()

Start from an element, then collect every sibling before it.

  1. Select the starting element(s).
  2. Call .prevAll() for every previous sibling.
  3. Optional: .prevAll("selector") to keep matching ones only.
  4. Remember order: nearest first.
  5. For wrap/append in document order, chain .uniqueSort() first.
SignatureSinceRole
.prevAll()1.2All preceding siblings (nearest first)
.prevAll(selector)1.2Same, 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

Cheat Sheet

Keep these facts handy while collecting previous siblings.

TopicDetail
DirectionAll siblings before the start
OrderNearest first (reverse document order)
SelectorKeeps matching previous siblings only
vs .prev()One neighbor vs all previous
vs .nextAll()Mirror — all following siblings
wrapAll tipUse .uniqueSort() first (3.7+)
Nearest match.prevAll(".x").first()

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.

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
Try It Yourself

How It Works

  1. Start. Select li.third-item.
  2. Collect. .prevAll() finds items 2 and 1.
  3. Mark. .prepend("[prev] ") labels each match.
  4. Scope. Items after the start are untouched.

Example 2 — Filter Previous Siblings

Keep only previous siblings that match a selector.

jQuery
$("li.third-item").prevAll(".mark").prepend("[match] ");
// only previous siblings with class "mark"
Try It Yourself

How It Works

  1. Walk. Look at every previous sibling.
  2. Test. Keep only those matching .mark.
  3. Unlike .prev(). Filtering can find matches further back.
  4. 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
Try It Yourself

How It Works

  1. Collect. .prevAll() builds nearest-first.
  2. Map. Read each sibling’s text.
  3. Join. Show the order clearly with arrows.
  4. Why it matters. Wrap/append follow this collection order.

Example 4 — Document Order with .uniqueSort()

Official tip: sort before order-sensitive methods like .wrapAll().

jQuery
var raw = $(".last").prevAll().map(function () {
  return $(this).text();
}).get().join(" → ");

var sorted = $(".last").prevAll().uniqueSort().map(function () {
  return $(this).text();
}).get().join(" → ");

$("#out").text("raw: " + raw + " | sorted: " + sorted);
// raw: Second → First | sorted: First → Second
Try It Yourself

How It Works

  1. Raw. Nearest-first: Second, then First.
  2. Sort. .uniqueSort() restores document order.
  3. Since. Instance .uniqueSort() needs jQuery 3.7+.
  4. Older. Use $.uniqueSort(collection) instead.

Example 5 — .prev() vs .prevAll()

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
Try It Yourself

How It Works

  1. prev. Only the immediate neighbor.
  2. prevAll. Every sibling before the start.
  3. Choose. One step → .prev(); all → .prevAll().
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.prevAll() Universal

Bottom line: Safe for everyday sibling collection. Remember reverse order before wrap/append.

Performance Notes

ApproachCostBest for
.prev()CheapestOne adjacent sibling
.prevAll() short listsCheapTypical UI lists / forms
.prevAll() huge sibling setsWatchPrefer .prevUntil() or a tighter selector

Cost scales with how many previous siblings exist. Prefer .prev() when one neighbor is enough.

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.

Next: .prevUntil() Method

Get previous siblings until (but not including) a stop element.

.prevUntil() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

17 people found this page helpful