Home TIL Element.closest()

Element.closest()

Element.closest() - Web APIs | MDN

The closest() method of the Element interface traverses the element and its parents (heading toward the document root) until it finds a node that matches the specified CSS selector.

If you’re trying to do a :has() style check to see if an element has a certain parent, then Element.closest() is what you need. Don’t bother with looping through parent nodes, like I almost did 😂