jQuery .closest() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.closest(selector) (since jQuery 1.3) finds the first element that matches by testing the current element, then walking up through ancestors. Returns a new jQuery set with at most one match per starting element. Does not change the DOM.

Syntax

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

jQuery
$("li.item-a").closest("ul");              // nearest ul ancestor
$("li.item-a").closest("li");              // matches self first
$("li.item-a").closest("ul", contextEl);   // limit search (since 1.4)
$(event.target).closest("li");             // common in click handlers

// closest → start at self, stop at first match (0 or 1)
// parents  → start at parent, collect all matches up the tree
CallWhat it does
selector / selection / elementWhat to match (string, jQuery, or DOM node)
contextSince 1.4 — match must be inside this element
Start pointCurrent element first (unlike .parents())
Return value0 or 1 element per start (jQuery object)

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

Parameters

ParameterDescription
Step 1Select a starting element (or use event.target).
Step 2Call .closest("selector") — tests self first, then parents.
Step 3Stop at the first match; empty set if none.

Return Value

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

jQuery Examples

Example 1 — Find the Nearest ul

Official idea: from item A, the first ul upward is ul.level-2.

jQuery
$("li.item-a").closest("ul").append(" [closest ul]");
Try It Yourself

How It Works

  1. Start. Begin at li.item-a.
  2. Test self. A is not a ul.
  3. Up. Parent ul.level-2 matches → stop.

Example 2 — Match the Element Itself

Official idea: .closest("li") from A selects A — not an ancestor li.

jQuery
$("li.item-a").closest("li").append(" [self]");
Try It Yourself

How It Works

  1. Self first. A is already an li.
  2. Stop. That is the nearest match.
  3. Contrast. .parents("li") would skip self and keep climbing.

Example 3 — Limit Search with context

Official idea: match must be a descendant of li#ii.

jQuery
var listItemII = document.getElementById("ii");
$("li.item-a").closest("ul", listItemII).append(" [in II]");
var outer = $("li.item-a").closest("#one", listItemII).length;
$("#log").text("#one with context II → length " + outer);
Try It Yourself

How It Works

  1. Context. listItemII is the boundary element.
  2. Inside. ul.level-2 is under #ii → allowed.
  3. Outside. #one is not a descendant of #ii → empty.

Frequently Asked Questions

.closest() starts at each element in the current set and walks up the DOM tree — testing the element itself first, then parent, grandparent, and so on — until it finds the first match for the selector. It returns a new jQuery object with at most one matched ancestor (or self) per starting element.
.closest() begins with the current element and stops at the first match (zero or one per start). .parents() starts at the parent (not self), collects every ancestor up to the document root, and can return many matches. Use .closest() for the nearest container; use .parents() for all matching ancestors.
Yes. Unlike .parents(), .closest() tests the starting element first. $("li.item-a").closest("li") matches item A itself before looking at any ancestor.
Since jQuery 1.4, .closest(selector, context) limits the upward search so a match must also be a descendant of the context DOM element. The search stops at that boundary.
In a click handler, event.target is often a nested span or icon. $(event.target).closest("li") walks up from the clicked node to the nearest li — even when the click did not land directly on the li.
No. Like other jQuery traversing methods, .closest() only queries the existing DOM and returns a new collection. It does not add, remove, or move elements.

Next: .offsetParent() Method

Get children including text and comment nodes — not just elements.

.offsetParent() 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.

10 people found this page helpful