Element.closest()
The DOM method that starts at an element and walks up through its ancestors, returning the first one, itself included, that matches a selector.
In more detail
It is the key to event delegation: from a click on an icon inside a button, closest('button.del') finds the button. It returns null when no ancestor matches.
Related terms
- ancestor — Any node above a given node in the DOM tree: its parent, the parent's parent and so on up to the document.
- Element.matches() — The DOM method that returns true if an element would be selected by a given CSS selector.
- event delegation — Handling events for many child elements with one listener on a shared ancestor, using event.target to find which child was involved.
Taught in
- Modern DOM Querying: querySelector, closest And matches — Frontend Architecture & Web Platform
- Event Delegation: Memory-Efficient Interaction Handlers — Frontend Architecture & Web Platform