jQuery .prevUntil() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.prevUntil() (since jQuery 1.4) gets all preceding siblings up to but not including a stop (selector, DOM node, or jQuery object). An optional filter keeps matching siblings only.

Syntax

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

jQuery
// Previous siblings until a dt (stop excluded)
$("#term-2").prevUntil("dt");

// Stop at a DOM node; keep only dd (since 1.6)
$("#term-3").prevUntil(document.getElementById("term-1"), "dd");

// No stop → same as .prevAll()
$("#term-2").prevUntil();
CallWhat it does
.prevUntil([selector][, filter])Stop at selector; optional filter
.prevUntil([element][, filter])Stop at Element or jQuery object
No stop / no matchSame as .prevAll() (unfiltered)

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

Parameters

ParameterDescription
[selector][Used in .prevUntil([selector][, filter]). Stop at selector; optional filter
filter]Used in .prevUntil([selector][, filter]). Stop at selector; optional filter
[element][Used in .prevUntil([element][, filter]). Stop at Element or jQuery object

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Previous Until a Selector

Official idea: from term 2, collect siblings until the previous dt.

jQuery
$("#term-2").prevUntil("dt").prepend("[until] ");
// marks definition 1-b and 1-a; term 1 is not marked
Try It Yourself

How It Works

  1. Start. Select #term-2.
  2. Walk. Move to each previous sibling.
  3. Stop. Halt when a dt is reached — exclude it.

Example 2 — Stop Plus Filter

Second argument keeps only matching siblings (e.g. dd).

jQuery
$("#term-3").prevUntil("#term-1", "dd").prepend("[dd] ");
// until term 1; keep dd only — skips term 2
Try It Yourself

How It Works

  1. Stop. Halt before #term-1.
  2. Filter. Keep only siblings matching dd.
  3. Skip. dt / other tags between are not kept.

Example 3 — Stop at a DOM Node

Since 1.6, pass an Element (or jQuery object) as the stop.

jQuery
var term1 = document.getElementById("term-1");
$("#term-3").prevUntil(term1, "dd").prepend("[node] ");
// same range as selector stop, with a live Element
Try It Yourself

How It Works

  1. Get node. Resolve the stop Element once.
  2. Pass it. First argument can be Element or jQuery.
  3. Filter. Optional second arg still works.

Frequently Asked Questions

.prevUntil() returns all preceding siblings up to but not including a stop element (selector, DOM node, or jQuery object). Optional second argument filters the collected siblings. Available since jQuery 1.4; element/jQuery stop since 1.6.
No. The stop is a fence — siblings before it are collected; the stop itself is excluded.
.prevAll() takes every previous sibling. .prevUntil(stop) stops when it reaches the stop. If the stop is missing or not supplied, .prevUntil() selects the same set as .prevAll() with no filter.
Nearest first (closest previous sibling to farthest) — the same reverse document order as .prevAll().
The second argument keeps only collected siblings that match that selector — e.g. .prevUntil("#term-1", "dd") keeps dd elements only.
Yes, since jQuery 1.6. Pass an Element or jQuery object as the first argument: .prevUntil(document.getElementById("term-1")).

Next: .siblings() Method

Get all siblings of each matched element — either side.

.siblings() Method 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.

15 people found this page helpful