jQuery .prev() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

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

Syntax

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

jQuery
// 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
CallWhat it does
.prev()Immediate previous sibling
.prev(selector)Same, only if sibling matches
No sibling / no matchEmpty jQuery object

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

Parameters

ParameterDescription
selectorUsed in .prev(selector). Same, only if sibling matches

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 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.

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.

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.

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 — All Previous Siblings

Get every preceding sibling — not just the immediate one.

.prevAll() Method — All Previous Siblings 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.

16 people found this page helpful