jQuery .prev() Method

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

What Is .prev()?

.prev() (since jQuery 1.0) gets the immediately preceding sibling of each matched element. An optional selector includes that sibling only if it matches.

Remember
// Immediate previous sibling
$("li.third-item").prev();

// Only if that sibling matches
$("p").prev(".selected");

// No previous / no match → empty set
$("#first").prev(); // length 0
NeedUse
One step backward.prev()
One step forward.next()
All previous siblings.prevAll()
Previous until a stop.prevUntil()
All siblings either side.siblings()

How to Use .prev()

Start from an element, then take one step to its previous sibling.

  1. Select the starting element(s).
  2. Call .prev() for the immediate previous sibling.
  3. Optional: .prev("selector") — keep it only if it matches.
  4. Act on the result (.prepend(), .text(), …).
  5. If .length is 0, there was no neighbor (or no match).
SignatureSinceRole
.prev()1.0Immediate previous sibling
.prev(selector)1.0Same, only if sibling matches
No sibling / no match—Empty jQuery object

Pseudocode

Pseudocode
result = empty jQuery object

for each matched element:
    sibling = immediate previous element sibling
    if no sibling:
        skip
    else if selector was given and sibling does not match:
        skip
    else:
        add sibling to result

return result

Cheat Sheet

Keep these facts handy while walking siblings.

TopicDetail
DirectionOne sibling backward
NodesElement siblings only (not text/comment)
SelectorFilters that one neighbor — no further scan
Empty resultSafe to chain; check .length if needed
vs .prevAll()All previous siblings
vs .next()Mirror — one step forward
ReturnsNew jQuery object (chainable)

Live Preview

Start from item 3 and step with .prev(), .next(), or a filtered prev.

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 the Previous List Item

Official idea: from item 3, select the immediate previous sibling.

jQuery
$("li.third-item").prev().prepend("[prev] ");
// list item 2 becomes "[prev] list item 2"
Try It Yourself

How It Works

  1. Start. Select li.third-item.
  2. Step. .prev() returns item 2.
  3. Mark. .prepend("[prev] ") labels it without CSS.
  4. Scope. Only the immediate neighbor is included.

Example 2 — Filter with a Selector

Include the previous sibling only when it matches.

jQuery
$("p").prev(".selected").prepend("[match] ");
// Only a previous sibling with class "selected" is marked
Try It Yourself

How It Works

  1. Start. Each p looks at its previous sibling.
  2. Test. Keep it only if it matches .selected.
  3. Fail. Non-matching neighbor → empty for that start.
  4. No walk. jQuery does not keep searching further back.

Example 3 — Step Backward on Click

Official “Go to Prev” idea: move a current marker one sibling at a time.

jQuery
var $curr = $("#start");
$curr.prepend("[here] ");

$("button").on("click", function () {
  $curr.text($curr.text().replace(/^\[here\]\s*/, ""));
  var $prev = $curr.prev();
  if (!$prev.length) {
    $curr.prepend("[here] ");
    $("#out").text("No previous sibling.");
    return;
  }
  $curr = $prev;
  $curr.prepend("[here] ");
  $("#out").text("Now at: " + $curr.text().replace("[here] ", ""));
});
Try It Yourself

How It Works

  1. Track. Keep a $curr reference.
  2. Click. Clear the old marker, then $curr.prev().
  3. Guard. If length is 0, stop — you hit the start.
  4. Update. Move the marker onto the new current element.

Example 4 — Empty When There Is No Neighbor

The first child has no previous sibling — result length is 0.

jQuery
var $p = $("#first").prev();
$("#out").text("length = " + $p.length);
// length = 0
Try It Yourself

How It Works

  1. Start. #first is the first sibling.
  2. Prev. There is no previous element sibling.
  3. Empty. Result has length === 0.
  4. Safe. Further chaining does nothing harmful.

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

One step vs all previous siblings 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 (item 2).
  2. prevAll. Every sibling before the start (2, then 1).
  3. Order. .prevAll() is nearest-first.
  4. Choose. One step → .prev(); all → .prevAll().

Edge Cases & Pitfalls

These trip up beginners most often with .prev().

filter

Selector does not walk further

.prev(".x") tests only the immediate sibling. For the nearest previous .x, use .prevAll(".x").first().

empty

First child → empty set

No previous element sibling means .length === 0. Check length before assuming a match.

nodes

Whitespace is ignored

.prev() skips text/comment nodes between tags. Only element siblings count.

parent

Not a parent or child

Sibling methods stay on the same level. Use .parent() / .children() for vertical moves.

Browser Support

.prev() has been part of jQuery since 1.0. Works wherever your loaded jQuery build runs.

✓ Stable · jQuery 1.0+

jQuery .prev()

Universal across browsers that run your jQuery version. Prefer current jQuery 3.x+ for teaching and production.

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
.prev() Universal

Bottom line: Safe for everyday sibling walking. Use .prevAll() when you need more than one step back.

Performance Notes

ApproachCostBest for
.prev()CheapOne adjacent sibling
.prev(selector)CheapOne neighbor + quick match test
.prevAll() on long listsWatchPrefer when you truly need every previous sibling

One step is inexpensive. Prefer .prev() over .prevAll() when a single neighbor is enough.

Key Takeaways

  • Role: one step to the immediately preceding sibling.
  • Filter: .prev(selector) tests that one neighbor only.
  • Empty: no sibling or no match → length 0; chaining is safe.
  • Choose: one step → .prev() · all → .prevAll() · forward → .next().

One line: .prev() is one sibling backward — optional selector filters that neighbor, nothing further.

Frequently Asked Questions

.prev() returns the immediately preceding sibling of each element in the matched set. It moves sideways in the DOM — not to parents or children. Available since jQuery 1.0.
.prev() returns at most one sibling — the direct previous neighbor. .prevAll() returns every preceding sibling. From list item 3, .prev() gives item 2 only; .prevAll() gives items 1 and 2.
.prev() looks backward one step; .next() looks forward one step. Same distance, opposite direction.
jQuery returns an empty object (length 0). Chaining is safe — .prev().addClass("x") does nothing when there is no neighbor.
No. It tests only the immediately preceding sibling. If that sibling does not match, the result is empty. Use .prevAll(selector).first() when you need the nearest previous match further back.
No. Like other sibling methods, .prev() considers element nodes only — whitespace text between tags is ignored.

Next: .prevAll() Method

Get every preceding sibling — not just the immediate one.

.prevAll() 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.

16 people found this page helpful