How is the closest element determined?



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


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

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