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
Fundamentals
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.
Concept
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.
Foundation
📝 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
Cheat Sheet
⚡ Quick Reference
Goal
Code
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()
Compare
📋 .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
Hands-On
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.
Paragraph whose immediate previous sibling has .selected → yellow on that sibling
Other paragraphs → no match (previous sibling lacks .selected)
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.
.prev() → red on item 2 only
.next() → blue on item 4 only
Same distance (one step), opposite direction
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.
Row immediately above .selected → .context-row class
First row (no previous tr) → empty .prev(), no change
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().
Applications
🚀 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.
Important
📝 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.
Compatibility
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 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
.prev()Universal
Bottom line: Safe in any jQuery project. Use .prev() for one step backward; use .prevAll() when you need every preceding sibling.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .prev()
One step backward among siblings.
5
Core concepts
←01
.prev()
-1 sibling
API
→02
.next()
Mirror
Compare
←←03
.prevAll()
All behind
Compare
?04
Selector
Immediate only
Filter
005
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.