How is the closest element determined?


#1

Question

In the context of this exercise, how is the closest element determined?

Answer

The closest() method will check the element’s ancestors, traversing up the DOM until it comes across the target element.

Every element can have only one parent, so there will be only one closest element at some layer of the DOM. In contrast, elements can have multiple siblings or children, but there can only be one parent per element.


FAQ: Traversing the DOM - Closest
#2

What’s the difference in between parent() and closest() method? When should we prefer one over another?