jQuery .prevAll() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
All preceding siblings

What You’ll Learn

The .prevAll() traversing method collects every preceding sibling before each matched element, optionally filtered by a selector. This tutorial covers the official list-item and div demos, filtered paragraph selection, comparisons with .prev() and .nextAll(), reverse document order, and the classic .prevAll().addBack() pattern.

01

All behind

Every prev sibling

02

Selector

.prevAll("p")

03

vs .prev()

Many vs one

04

vs .nextAll()

Back / forward

05

+ .addBack()

Include self

06

Since 1.2

Core API

Introduction

When one step backward is not enough — you need every sibling that comes before the current element — reach for .prevAll(). List items above a cutoff, divs before the last box, or wizard steps already completed all fit this pattern.

Available since jQuery 1.2, .prevAll([selector]) constructs a new jQuery object from all preceding siblings of each element in the current set. It stays on the same level of the DOM tree — siblings only, not children or ancestors — and optionally filters results with a selector expression. Results arrive in reverse document order, beginning with the closest sibling.

Understanding the .prevAll() Method

Given a jQuery object representing a set of DOM elements, .prevAll() walks backward through each element’s sibling list. Every element sibling that appears before the current node is included in the result. When you pass a selector, only matching siblings among those behind are kept.

This is broader than .prev(), which stops at the immediate neighbor, and directional — unlike .siblings(), which can return both preceding and following siblings. It is the mirror of .nextAll(). Pair .prevAll() with .addBack() when you need the starting element plus everything before it, and with .uniqueSort() when APIs like .wrapAll() require document order (jQuery 3.7+).

💡
Beginner Tip

$("li.third-item").prev() returns item 2 only. $("li.third-item").prevAll() returns items 1 and 2. One neighbor vs the entire head of siblings.

📝 Syntax

General form of .prevAll:

jQuery
.prevAll( [ selector ] )

Parameters

  • selector (optional) — a string containing a selector expression. Only preceding siblings that match this selector are included in the result.

Return value

  • A new jQuery object containing all preceding siblings of each element in the current set (filtered when a selector is provided), in reverse document order — closest sibling first.

Official jQuery API list example

jQuery
$( "li.third-item" ).prevAll().css( "background-color", "red" );

// Red background on list items 1 and 2 — all siblings before item 3

⚡ Quick Reference

GoalCode
All preceding siblings$("li.end").prevAll()
Preceding siblings matching a tag$("div").last().prevAll("p")
Immediate previous sibling only$("li").prev()
All following siblings$("li").nextAll()
Starting item plus everything before$("li.third-item").prevAll().addBack()
Restore document order before wrapAll$(".last").prevAll().uniqueSort().wrapAll("<div>")

📋 .prevAll() vs .prev() vs .nextAll() vs .siblings()

Four sibling methods — same parent level, different scope and direction.

.prevAll()
all backward

Every preceding sibling before current

.prev()
-1 backward

One immediately preceding sibling

.nextAll()
all forward

Every following sibling after current

.siblings()
both ways

All siblings except self — optional filter

Examples Gallery

Examples 1–3 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.

📚 Getting Started

Official jQuery demos for all preceding-sibling traversal.

Example 1 — Official Demo: Highlight All List Items Before the Third

From item 3, style every preceding sibling — the core official list lesson.

jQuery
$( "li.third-item" ).prevAll().css( "background-color", "red" );
Try It Yourself

How It Works

Item 3’s preceding siblings are items 1 and 2. .prevAll() returns both — unlike .prev(), which would stop at item 2 alone. Order in the jQuery object is reverse document order: item 2 first, then item 1.

Example 2 — Official Demo: Class on All Divs Before the Last

Locate every div sibling before the last div and add class before.

jQuery
$( "div" ).last().prevAll().addClass( "before" );
Try It Yourself

How It Works

.last() picks the final div. .prevAll() collects every div sibling before it — skipping the last but including the rest at the same parent level.

Example 3 — Filter Preceding Paragraph Siblings

From the last child, keep only paragraph siblings — .prevAll("p") among mixed div and p elements.

jQuery
$( ":last-child" ).prevAll( "p" ).addClass( "before" );
Try It Yourself

How It Works

.prevAll("p") scans every sibling behind the last child but keeps only paragraphs. Div siblings between them are ignored because they do not match the selector.

📈 Practical Patterns

Compare scope with .prev(), then merge the starting element with .addBack().

Example 4 — .prev() vs .prevAll() on the Same List

See how one step differs from the full head of siblings.

jQuery
$( "#prev-demo li.third-item" ).prev().css( "color", "red" );

$( "#prevall-demo li.third-item" ).prevAll().css( "color", "blue" );



// .prev() → item 2 | .prevAll() → items 1 and 2
Try It Yourself

How It Works

Both methods start from item 3, but .prev() stops at the first neighbor while .prevAll() continues through every earlier sibling.

Example 5 — Include the Starting Item with .prevAll().addBack()

Highlight item 3 plus all preceding siblings — mirror of the official .nextAll().addBack() pattern.

jQuery
$( "li.third-item" ).prevAll().addBack()

  .css( "background-color", "red" );
Try It Yourself

How It Works

.prevAll() alone returns items 1–2. .addBack() merges item 3 from the stack — perfect for “this and everything before” UI ranges.

🚀 Common Use Cases

  • Disable completed steps$(".step.current").prevAll(".step").addClass("done").
  • Gray out earlier form fields — before a cutoff input, style all preceding inputs in the same group.
  • Style leading table rows — highlight or remove every row before a selected anchor row.
  • Tag all siblings before the last — official pattern: $("div").last().prevAll().addClass("before").
  • Filter by type behind$(":last-child").prevAll("p") for paragraphs only among mixed siblings.
  • Range highlight with addBack.prevAll().addBack() for starting item plus head.
  • Fix order before wrapAll.prevAll().uniqueSort().wrapAll("<div>") (jQuery 3.7+).

🧠 How .prevAll() Collects Preceding Siblings

1

Start with source elements

jQuery object holds one or more DOM nodes.

Input
2

Walk sibling chain backward

Collect every element sibling before each source node — closest first.

Traverse
3

Apply selector filter

If provided, keep only siblings that match.

Filter
4

Return & chain

New jQuery object in reverse order — pushes stack for .addBack() or .end().

📝 Notes

  • Available since jQuery 1.2 — optional selector argument filters preceding siblings.
  • Returns all preceding siblings — not the immediate neighbor only (that is .prev()).
  • Results are in reverse document order — closest sibling first, farthest last.
  • Does not include the starting element — use .addBack() to merge it back in.
  • Reverse order can break .wrapAll() — call .uniqueSort() first (jQuery 3.7+); older versions use $.uniqueSort().
  • Text nodes and comments between elements are skipped — element siblings only.
  • Pushes onto jQuery’s internal stack — pair with .end() to restore the previous set.
  • Empty result when no preceding siblings exist — safe to chain without errors.

Browser Support

.prevAll() has been part of jQuery since 1.2+. It relies on standard sibling traversal with no browser-specific behavior beyond jQuery itself.

jQuery 1.2+

jQuery .prevAll()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: loop previousElementSibling — jQuery adds selector filtering, collection semantics, and reverse-order collection.

100% With jQuery loaded
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 in any jQuery project. Use .prevAll() for every preceding sibling; use .prev() when one step is enough. Call .uniqueSort() before .wrapAll() when order matters.

Conclusion

The jQuery .prevAll() method returns every preceding sibling of each element in the current collection. It is the natural choice when you need the full head of siblings — not just the previous neighbor.

Remember the official list lesson: from item 3, items 1 and 2 get the red background. Use .prev() for one step, .nextAll() for everything after, and .prevAll().addBack() when the starting element must be included in the range. When chaining .wrapAll(), sort first with .uniqueSort().

💡 Best Practices

✅ Do

  • Use .prevAll() when you need every sibling behind
  • Pass a selector to limit results to a specific tag or class
  • Chain .addBack() when the starting element belongs in the range
  • Call .uniqueSort() before .wrapAll() on .prevAll() output
  • Call .end() when continuing on the original matched set
  • Compare mentally with .prev() — one vs many

❌ Don’t

  • Use .prevAll() when only the immediate neighbor matters — use .prev()
  • Expect the starting element in the result — add .addBack() if needed
  • Assume document order from .prevAll() — results are reverse order
  • Chain .wrapAll() directly on .prevAll() without sorting
  • Confuse siblings with descendants — use .find() for inner elements
  • Forget stack behavior — narrowing steps affect .end() and .addBack()

Key Takeaways

Knowledge Unlocked

Five things to remember about .prevAll()

Every sibling behind on the same level.

5
Core concepts
02

.prev()

One step

Compare
→→ 03

.nextAll()

Mirror

Compare
+ 04

.addBack()

Include self

Stack
05

Order

Reverse DOM

Sort

❓ Frequently Asked Questions

.prevAll() returns every preceding sibling before each element in the current jQuery collection — not just the immediate neighbor. From list item 3 in a five-item list, .prevAll() gives items 1 and 2. Results are in reverse document order: the closest sibling comes first.
.prev() returns at most one sibling — the direct previous neighbor. .prevAll() returns all siblings that come before the current element. Use .prev() for one step backward; use .prevAll() when you need everything behind.
.prevAll() collects preceding siblings; .nextAll() collects following siblings. They are mirror-image methods — same scope (all siblings in one direction), opposite direction. .prevAll() returns elements in reverse document order; .nextAll() returns them in forward order.
Yes. .prevAll(selector) returns only the preceding siblings that match the selector. Unlike .prev(selector), which tests only the immediate neighbor, .prevAll() scans every sibling before the current element and keeps those that match.
jQuery returns an empty jQuery object — length 0. Chaining still works safely. The first list item, opening table row, or first div in a container typically yields an empty .prevAll() result.
.prevAll() returns siblings in reverse document order (closest first). That order can break APIs like .wrapAll() that process nodes sequentially. Call .uniqueSort() before .wrapAll() (jQuery 3.7+) to restore document order. After .prevAll(), .addBack() merges the starting element back in — $('li.third-item').prevAll().addBack() highlights items 1, 2, and 3.
Did you know?

Because .prevAll() returns siblings in reverse document order, chaining .wrapAll() directly can produce unexpected HTML — the second sibling gets wrapped before the first. The official jQuery API docs recommend calling .uniqueSort() on the .prevAll() output first (jQuery 3.7+). This is the mirror-image concern of .nextAll(), which returns forward order by default.

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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.

6 people found this page helpful