jQuery .prevAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.prevAll() (since jQuery 1.2) gets all preceding siblings of each matched element, optionally filtered by a selector — nearest previous sibling first.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// 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("
");
CallWhat it does
.prevAll()All preceding siblings (nearest first)
.prevAll(selector)Same, filtered by selector
No previous siblingsEmpty jQuery object

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
selectorUsed in .prevAll(selector). Same, filtered by selector

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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.

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 — Previous Siblings Until Stop

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

.prevUntil() Method — Previous Siblings Until Stop tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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