How is the closest element determined?

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.

2 Likes

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

5 Likes