JavaScript Element closest() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.closest() method starts at the element and walks up through parents toward the document root until it finds a node that matches a CSS selector (MDN). It returns that element (or null). The element itself is tested first.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.closest. Start with this line.

JavaScript
el.closest(selectors)
CallWhat it does
el.closest(selectors)Primary signature.
el.closest(...)Call with the arguments your case needs.

Built into the browser. Call closest(...) on an Element — no import required.

Library declarations
JavaScript
el.closest(selectors)

Parameters

ParameterDescription
Starting elementRequired. Where the upward walk begins
selectorsRequired. Valid CSS selector string

Return Value

Return ValueDescription
*Matching Element, or null

JavaScript Examples

Example 1 — Find an Ancestor

From an inner element, find the nearest ancestor with a given id.

JavaScript
const el = document.getElementById("div-03");
const out = document.getElementById("out");

const found = el.closest("#div-02");
out.textContent = found ? found.id : "null";
Try It Yourself

How It Works

  1. Start inside the tree. #div-03 is the current element.
  2. Pass a selector. "#div-02" is valid CSS.
  3. Walk up. Self fails; the parent #div-02 matches.
  4. Use the result. Null-check before reading id.

Example 2 — Match Self

If the element itself matches, closest() returns it immediately.

JavaScript
const el = document.getElementById("card");
const out = document.getElementById("out");

const found = el.closest(".card");
out.textContent = found === el ? "matched self" : "ancestor";
Try It Yourself

How It Works

  1. Call on the candidate. The walk starts at el.
  2. Test self first. MDN: the element itself can be the match.
  3. No parent climb needed. A self-match returns immediately.
  4. Compare references. found === el confirms self.

Example 3 — Event Delegation

From a click target, find the nearest .item button (or ignore the click).

JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");

list.addEventListener("click", (event) => {
  const item = event.target.closest(".item");
  if (!item || !list.contains(item)) return;
  out.textContent = "clicked " + item.textContent;
});
Try It Yourself

How It Works

  1. Listen on a parent. One handler covers many children.
  2. Start from event.target. The real click node may be nested text/span.
  3. Climb to .item. closest finds the actionable ancestor.
  4. Guard with contains. Ignore matches outside this list.

Browser Support

Element.closest() is Baseline Widely available on MDN (across browsers since April 2017). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.closest()

Standard upward CSS-selector walk for ancestors (and self). Widely available since 2017.

2017+ Widely available
Google Chrome 41+
Supported
Mozilla Firefox 35+
Supported
Apple Safari 9+
Supported
Microsoft Edge 15+
Supported
Opera 28+
Supported
Internet Explorer Not supported — polyfill
Unavailable
closest() Baseline

Bottom line: Safe for modern browsers. Prefer closest over manual parentElement loops.

Frequently Asked Questions

It starts at the element you call it on and walks up through parents toward the document root (MDN). It returns the first element (including itself) that matches the CSS selector, or null if none match.
No. MDN marks Element.closest() as Baseline Widely available (across browsers since April 2017). It is not Deprecated, Experimental, or Non-standard.
Yes. The element you call closest() on is tested first. If it matches the selector, that element is returned.
null. Always check the result before using properties or methods on it.
matches() tests only the current element. closest() tests the element and then each ancestor until one matches or the tree ends.
A SyntaxError DOMException is thrown if the selector string is not valid CSS.

Next: computedStyleMap()

Read computed CSS values as a Typed OM StylePropertyMapReadOnly.

computedStyleMap() →

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.

22 people found this page helpful