jQuery .prevUntil() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Preceding siblings until stop

What You’ll Learn

The .prevUntil() traversing method collects preceding siblings up to a stop boundary — every sibling before the current element until (but not including) a matched selector, DOM node, or jQuery object. This tutorial covers the official definition-list demo, DOM-node stop with filter, comparisons with .prevAll(), and practical section-range patterns.

01

Stop boundary

Up to, not including

02

Selector stop

.prevUntil("dt")

03

DOM stop

Since jQuery 1.6

04

Filter arg

, "dd"

05

vs .prevAll()

Range vs all

06

Since 1.4

Core API

Introduction

Sometimes you need siblings in a range going backward — definitions before one term but not the previous heading, list items until a separator, or table rows until a header row. .prevAll() grabs everything behind; .prevUntil() stops at a boundary you define.

Available since jQuery 1.4 (DOM-node stop since 1.6), .prevUntil(stop [, filter]) walks backward through preceding siblings and halts before the first match of the stop argument. An optional filter narrows which siblings within that range are kept — ideal when terms and definitions share the same parent as mixed sibling types.

Understanding the .prevUntil() Method

Given a jQuery object representing a set of DOM elements, .prevUntil() examines each element’s preceding siblings in order (closest first). Siblings are added to the result until jQuery reaches one that matches the stop selector, DOM node, or jQuery object — that stop sibling is excluded. Siblings before the stop are never reached beyond the boundary.

Think of it as a sliding window among siblings: start just before your element, end right before the stop. Without a stop or when the stop is never found, the window extends to the beginning — equivalent to .prevAll(). The second filter argument lets you keep only certain tags or classes within that window.

💡
Beginner Tip

$("#term-2").prevUntil("dt") selects definition dd elements for term 1 only — it stops before the previous dt (term 1). The stop dt itself is never selected.

📝 Syntax

General forms of .prevUntil:

jQuery
.prevUntil( [ selector ] [, filter ] )

.prevUntil( [ element ] [, filter ] )

Parameters

  • selector — a string containing a selector expression indicating where to stop matching preceding siblings (stop element excluded).
  • element — a DOM node or jQuery object indicating the stop boundary (since jQuery 1.6).
  • filter (optional) — a selector expression; only siblings in the range that match this filter are included.

Return value

  • A new jQuery object containing preceding siblings up to but not including the stop match, optionally filtered.

Official jQuery API definition-list example

jQuery
$( "#term-2" ).prevUntil( "dt" ).css( "background-color", "red" );

// Red on definition 1-a, 1-b, 1-c, 1-d — stops before term-1 dt

⚡ Quick Reference

GoalCode
Preceding siblings until previous dt$("#term-2").prevUntil("dt")
Stop at a DOM node, filter to dd$("#term-3").prevUntil(term1, "dd")
All preceding siblings (no stop)$("li").prevAll()
Items until a separator li$("li.end").prevUntil("li.separator")
Rows until a header tr$("tr.data").prevUntil("tr.section-head")
Mirror going forward$("li").nextUntil("li.marker")

📋 .prevUntil() vs .prevAll() vs .prev()

Three backward sibling methods — one step, full tail, or bounded range.

.prevUntil()
until stop

Preceding siblings up to boundary — stop excluded

.prevAll()
all behind

Every preceding sibling — no stop point

.prev()
-1 only

One immediately preceding sibling

.nextUntil()
forward

Same range logic — following siblings until stop

Examples Gallery

Examples 1–2 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 bounded sibling ranges.

Example 1 — Official Demo: Definitions Until the Previous dt

From term 2, highlight sibling definitions before it — stop before the previous term heading.

jQuery
$( "#term-2" ).prevUntil( "dt" ).css( "background-color", "red" );
Try It Yourself

How It Works

Before #term-2, jQuery collects preceding siblings until the previous dt (#term-1). Those four dd elements are in range; the stop dt is excluded.

Example 2 — Official Demo: Stop at DOM Node with dd Filter

From term 3, green text on every dd until (not including) term 1.

jQuery
var term1 = document.getElementById( "term-1" );

$( "#term-3" ).prevUntil( term1, "dd" ).css( "color", "green" );
Try It Yourself

How It Works

The DOM node #term-1 is the stop boundary. The "dd" filter keeps only definitions in the range — intermediate dt elements are skipped because they do not match the filter.

📈 Practical Patterns

Compare with .prevAll(), then apply stop boundaries in lists and tables.

Example 3 — .prevUntil() vs .prevAll() on the Same List

See how a stop boundary limits the range compared to selecting the full preceding tail.

jQuery
$( "#until-demo li.fifth-item" ).prevUntil( "li.stop" ).css( "color", "red" );

$( "#all-demo li.fifth-item" ).prevAll().css( "color", "blue" );



// .prevUntil("li.stop") → items 3 and 4 only | .prevAll() → items 1, 2, 3, and 4
Try It Yourself

How It Works

Item 2 has class stop. .prevUntil("li.stop") from item 5 collects only items 4 and 3; item 2 is the excluded boundary. .prevAll() ignores the boundary and takes all four preceding items.

Example 4 — List Items Preceding Until a Separator

Hide or style siblings in one section without affecting the previous group.

jQuery
$( "li.section-end" ).prevUntil( "li.section-start" )

  .addClass( "in-section" );
Try It Yourself

How It Works

Section markers act as start and stop bookends. .prevUntil() selects the content between them — the start marker itself is excluded from the result.

Example 5 — Table Rows Preceding Until a Section Header

Stripe or highlight data rows belonging to one group before the previous header row.

jQuery
$( "tr.group-head" ).prevUntil( "tr.group-head" )

  .addClass( "group-row" );
Try It Yourself

How It Works

Each header row ends a range going backward; the previous header is the stop. Data rows between two tr.group-head elements get the class — a common grouped-table pattern without nested markup.

🚀 Common Use Cases

  • Definition lists$("dt.term").prevUntil("dt") for definitions under the previous term.
  • Sectioned lists — items between .section-start and .section-end markers.
  • Grouped table rows — data rows until the previous tr.group-head.
  • Wizard steps — steps in the current phase until a .phase-divider sibling.
  • Mixed sibling filter.prevUntil(stop, "dd") when only one tag type matters in the range.
  • DOM-node boundary — stop at a known element reference from JavaScript.

🧠 How .prevUntil() Builds a Sibling Range

1

Start with source elements

jQuery object holds one or more DOM nodes.

Input
2

Walk backward sibling by sibling

Collect each preceding element sibling in order (closest first).

Traverse
3

Stop before boundary match

Halt when stop selector, DOM node, or jQuery object matches — exclude it.

Boundary
4

Filter & return

Apply optional filter — return bounded sibling set for chaining.

📝 Notes

  • Available since jQuery 1.4 — DOM-node or jQuery stop since 1.6.
  • The stop element is never included — only siblings strictly before it in the chain.
  • If the stop is not found, behavior matches .prevAll() (with optional filter).
  • The optional filter applies to siblings within the range — not to the stop element.
  • Text nodes and comments between elements are skipped — element siblings only.
  • Mirror method: .nextUntil() — same range logic going forward.

Browser Support

.prevUntil() has been part of jQuery since 1.4+ (DOM stop since 1.6). It relies on standard sibling traversal with no browser-specific behavior beyond jQuery itself.

jQuery 1.4+

jQuery .prevUntil()

Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. No native single-method equivalent — combine previousElementSibling loops or use .prevUntil() for clarity.

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

Bottom line: Safe in any jQuery project. Use .prevUntil() for bounded sibling ranges; use .prevAll() when no stop boundary exists.

Conclusion

The jQuery .prevUntil() method returns preceding siblings up to but not including a stop boundary. It fills the gap between .prev() (one step) and .prevAll() (everything behind) when your DOM has natural section markers.

Remember the official definition-list lesson: $("#term-2").prevUntil("dt") styles only term 1’s definitions. Pass a DOM node as the stop and a filter selector when mixed sibling types share the same parent.

💡 Best Practices

✅ Do

  • Use clear stop markers in markup — dt, .section-start, header rows
  • Pass a filter when only one sibling type belongs in the range
  • Remember the stop element is excluded from results
  • Use DOM-node stop when you already hold an element reference
  • Compare with .prevAll() — range vs full tail

❌ Don’t

  • Expect the stop boundary inside the returned set
  • Use .prevUntil() when you need every preceding sibling — use .prevAll()
  • Assume the filter searches beyond the stop — range ends at the boundary
  • Confuse siblings with descendants — nested content needs .find()
  • Forget that a missing stop falls back to .prevAll() behavior

Key Takeaways

Knowledge Unlocked

Five things to remember about .prevUntil()

Preceding siblings with a stop boundary.

5
Core concepts
02

Stop

Excluded

Boundary
←← 03

.prevAll()

No stop

Compare
dd 04

Filter

2nd arg

Filter
DOM 05

Node stop

Since 1.6

API

❓ Frequently Asked Questions

.prevUntil() returns preceding siblings of each element in the current set, stopping before the first sibling that matches a selector, DOM node, or jQuery object. The stop element itself is never included. It defines a bounded range of siblings behind — not the entire preceding tail.
.prevAll() returns every preceding sibling with no stop point. .prevUntil(stop) returns siblings only until (but not including) the stop match. If no stop is supplied or the stop is never found, .prevUntil() behaves like .prevAll().
No. The element matched by the stop selector, DOM node, or jQuery object is excluded. Only siblings between the starting element and that stop boundary are returned.
Yes, since jQuery 1.6. Pass a raw DOM node or jQuery object as the first argument — for example prevUntil(document.getElementById('term-1')). The traversal stops before that element.
prevUntil(stop, filter) limits which siblings in the range are kept. Only elements between start and stop that also match the filter selector are included — useful when mixed sibling types sit in the same range.
jQuery collects all preceding siblings — the same result as .prevAll() with no filter. The range runs to the start of the sibling list because no stop boundary was encountered.
Did you know?

.prevUntil() and .nextUntil() were added together in jQuery 1.4 as mirror-image range selectors — one walks backward through preceding siblings, the other forward through following siblings, both stopping before a boundary element. When DOM-node stops arrived in 1.6, both methods gained the same upgrade.

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