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
Fundamentals
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.
Concept
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.
Foundation
📝 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
list items 1 and 2 → red background
Items 3, 4, 5 → unchanged
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.
Preceding p elements before last child → .before class
Intervening div siblings → skipped by "p" filter
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.
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().
Important
📝 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.
Compatibility
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 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 in any jQuery project. Use .prevAll() for every preceding sibling; use .prev() when one step is enough. Call .uniqueSort() before .wrapAll() when order matters.
Wrap Up
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().
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()
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .prevAll()
Every sibling behind on the same level.
5
Core concepts
←←01
.prevAll()
All behind
API
←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.