jQuery .prev() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Previous sibling

What You’ll Learn

The .prev() traversing method moves sideways in the DOM — it returns the immediately preceding sibling of each matched element, optionally filtered by a selector. This tutorial covers the official list-item and Go to Prev demos, selector filtering, comparisons with .next(), .prevAll(), and .siblings(), and practical row-navigation patterns.

01

One step

Previous neighbor only

02

Selector

.prev(".x")

03

vs .next()

Back / forward

04

vs .prevAll()

One vs many

05

Empty safe

No neighbor OK

06

Since 1.0

Core API

Introduction

Siblings share the same parent — think of list items in a ul, table rows in a tbody, or divs in a horizontal gallery. When you need the element right before your current selection, reach for .prev().

Available since jQuery 1.0 (selector filtering since 1.4), .prev([selector]) constructs a new jQuery object from the immediately preceding sibling of each element in the current set. It does not descend into children or climb to ancestors — it stays on the same level of the tree, one step backward.

Understanding the .prev() Method

Given a jQuery object representing a set of DOM elements, .prev() looks at each element’s previousElementSibling in the DOM. If that neighbor exists, it is added to the result. When you pass a selector, jQuery includes the neighbor only if it matches — otherwise the result for that element is empty.

This is narrower than .prevAll(), which collects every preceding sibling, and narrower than .siblings(), which can return both directions. .prev() is the precision tool for “the very previous one” — perfect for backward step wizards, walking a gallery with a Go to Prev button, or highlighting the row above a selection.

💡
Beginner Tip

$("li.third-item").prev() returns list item 2 only. $("li.third-item").prevAll() returns items 1 and 2. One step vs everything behind.

📝 Syntax

General form of .prev:

jQuery
.prev( [ selector ] )

Parameters

  • selector (optional) — a string containing a selector expression. The immediately preceding sibling is included only if it matches this selector.

Return value

  • A new jQuery object containing the immediately preceding sibling of each element in the current set (filtered when a selector is provided).

Official jQuery API list example

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

// Red background on list item 2 — the very previous sibling before item 3

⚡ Quick Reference

GoalCode
Immediate previous sibling$("li.active").prev()
Previous sibling only if it matches$("p").prev(".highlight")
Walk backward through div siblings$curr = $curr.prev()
All preceding siblings$("li.start").prevAll()
Following sibling$("li").next()
Both directions$("li").siblings()

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

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

.prev()
-1 backward

One immediately preceding sibling

.next()
+1 forward

One immediately following sibling

.prevAll()
all behind

Every preceding sibling before 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 immediate previous-sibling traversal.

Example 1 — Official Demo: Highlight the Previous List Item

From the third list item, style the immediately preceding sibling — the core official lesson.

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

How It Works

Item 3’s previous sibling is item 2. .prev() returns exactly that one element — item 1 is ignored because it is not the immediate neighbor.

Example 2 — Official Demo: Go to Prev Button — Walk Backward Through Div Siblings

Each click moves the highlight one div backward using $curr = $curr.prev().

jQuery
var $curr = $( "#start" );

$curr.css( "background", "#f99" );

$( "button" ).on( "click", function() {

  $curr = $curr.prev();

  $( "div" ).css( "background", "" );

  $curr.css( "background", "#f99" );

});
Try It Yourself

How It Works

Reassigning $curr to $curr.prev() walks backward one sibling at a time — the mirror of the official Go to Next pattern with .next().

Example 3 — Official Demo: Keep Only Previous Siblings Matching .selected

Filter the immediate previous sibling — include it only when it has class selected.

jQuery
$( "p" ).prev( ".selected" ).css( "background", "yellow" );
Try It Yourself

How It Works

jQuery checks only the direct previous sibling. If it lacks .selected, that paragraph contributes nothing to the result — jQuery does not skip back to a later matching sibling.

📈 Practical Patterns

Compare mirror methods, then navigate table rows with .prev().

Example 4 — .prev() vs .next() Mirror Comparison

From the same starting item, see backward vs forward one-step traversal.

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

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



// .prev() → item 2 | .next() → item 4
Try It Yourself

How It Works

.prev() and .next() are mirror-image methods. From item 3, backward lands on item 2 and forward lands on item 4 — never both at once from a single call.

Example 5 — Highlight the Row Above the Selected Table Row

Real-world pattern: after clicking a row, style the previous tr sibling for context or breadcrumb hints.

jQuery
$( "tr.selected" ).prev().addClass( "context-row" );
Try It Yourself

How It Works

Table rows in a tbody are siblings. .prev() moves to the single row above — ideal for showing parent context without selecting every preceding row via .prevAll().

🚀 Common Use Cases

  • Backward gallery navigation — official pattern: $curr = $curr.prev() on each Go to Prev click.
  • Step wizards (reverse)$(".step.active").prev(".step").addClass("active") when steps are adjacent siblings.
  • Context rows in tables — highlight the row above the current selection for hierarchy hints.
  • Form field labels — update the element immediately before an input when validation state changes.
  • Accordion panels — close or style the panel immediately before a clicked header.
  • Chained navigation.prev().prev() skips two siblings when layout guarantees adjacency.

🧠 How .prev() Finds the Immediate Sibling

1

Start with source elements

jQuery object holds one or more DOM nodes.

Input
2

Read previous sibling

For each node, get previousElementSibling.

Traverse
3

Apply selector filter

If provided, keep neighbor only when it matches.

Filter
4

Return & chain

New jQuery object — empty when no matching neighbor exists.

📝 Notes

  • Available since jQuery 1.0 — optional selector argument since 1.4.
  • Returns at most one sibling per source element — not all preceding siblings.
  • Text nodes and comments between elements are skipped — jQuery uses element siblings only.
  • With a selector, only the immediate previous sibling is tested — no further scanning.
  • Empty result returns an empty jQuery object — safe to chain without errors.
  • Pair with .next() for forward navigation and .prevAll() when you need the full head of siblings.

Browser Support

.prev() has been part of jQuery since 1.0+ (selector form since 1.4). It relies on standard sibling traversal with no browser-specific behavior beyond jQuery itself.

jQuery 1.0+

jQuery .prev()

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

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

Bottom line: Safe in any jQuery project. Use .prev() for one step backward; use .prevAll() when you need every preceding sibling.

Conclusion

The jQuery .prev() method returns the immediately preceding sibling of each element in the current collection. It is the simplest way to move one step backward among elements that share the same parent.

Remember the official list lesson: from item 3, only item 2 gets the red background. Use .prevAll() when you need everything behind, .next() to step forward, and .prev(selector) when the very previous neighbor must match before you act on it.

💡 Best Practices

✅ Do

  • Use .prev() when layout guarantees an adjacent sibling
  • Check .prev().length before acting when the neighbor may not exist
  • Pair with semantic HTML — labels beside inputs, rows in tables
  • Use .prev(selector) to require a specific neighbor type
  • Prefer .prevAll(selector).last() when matches may be further back

❌ Don’t

  • Use .prev() when you need all preceding siblings — use .prevAll()
  • Expect .prev(".x") to skip non-matching neighbors
  • Confuse siblings with children — use .children() or .find() for descendants
  • Assume text nodes count — only element siblings are returned
  • Chain .prev().prev() unless the DOM structure is stable and documented

Key Takeaways

Knowledge Unlocked

Five things to remember about .prev()

One step backward among siblings.

5
Core concepts
02

.next()

Mirror

Compare
←← 03

.prevAll()

All behind

Compare
? 04

Selector

Immediate only

Filter
0 05

Empty

Safe chain

Edge case

❓ Frequently Asked Questions

.prev() returns the immediately preceding sibling of each element in the current jQuery collection. It moves sideways along the DOM tree — not down into children and not up to parents. Without a selector, it always includes the very previous sibling element if one exists.
.prev() returns at most one sibling — the direct previous neighbor. .prevAll() returns every preceding sibling before that point. From list item 3, .prev() gives item 2 only; .prevAll() gives items 1 and 2.
.prev() looks backward to the preceding sibling; .next() looks forward to the following sibling. They are mirror-image methods — same distance (one step), opposite direction.
jQuery returns an empty jQuery object — length 0. Chaining still works safely: .prev().addClass('x') simply does nothing when no neighbor exists. Test with .prev().length before acting if needed.
No. .prev(selector) checks only the immediately preceding sibling. If that sibling does not match the selector, the result is empty — jQuery does not keep scanning further siblings. Use .prevAll(selector).last() or .siblings(selector).last() when you need the previous match further back.
Yes. jQuery runs .prev() for each element in the current set and unions the results. $('p').prev() returns the previous sibling of every paragraph in the collection — duplicates are removed automatically in the jQuery object.
Did you know?

Before jQuery 1.4, .prev() had no selector argument — developers wrote .prev().filter(".selected") to test the neighbor. The optional selector parameter folded that two-step pattern into one call, matching how .prevAll(selector) already worked for multiple siblings.

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