DOM Element.closest() Method
The closest() method searches up the tree from an element and returns the nearest ancestor, or the element itself, that matches a CSS selector.
Description
The closest() method starts at the element and travels up through its ancestors, returning the first one that matches the selectors, including the element itself. It returns null if none matches.
It's the key to event delegation: with one listener on a container, event.target.closest('button') finds the button that was clicked even if the click landed on an icon or text inside it.
Because it includes the element itself, el.closest('.card') returns el when it already has the card class. It only looks upwards, never at siblings or descendants, and stops at the document root.
Syntax
element.closest(selectors)Parameters
- selectorsA string of one or more CSS selectors.
Return Value
The nearest matching ancestor (or the element itself), or null if none matches.
Example
Browser Support
The following information will show you the current browser support for the Element.closest() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





