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)
Call
What 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
Parameter
Description
Starting element
Required. Where the upward walk begins
selectors
Required. Valid CSS selector string
Return Value
Return Value
Description
*
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";
Start inside the tree.#div-03 is the current element.
Pass a selector."#div-02" is valid CSS.
Walk up. Self fails; the parent #div-02 matches.
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";
Listen on a parent. One handler covers many children.
Start from event.target. The real click node may be nested text/span.
Climb to .item.closest finds the actionable ancestor.
Guard with contains. Ignore matches outside this list.
Compatibility
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 Chrome41+
Supported
Mozilla Firefox35+
Supported
Apple Safari9+
Supported
Microsoft Edge15+
Supported
Opera28+
Supported
Internet ExplorerNot 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.