The .closest() method will search for the first ancestor of the selector

It travels up the DOM from the first of the elements matching the selector in the DOM. It will continue traveling up the DOM tree until it finds the first element that matches the parameter in the parentheses. The value of .closest() will then be no elements or one element, depending on what it found in the DOM tree.

<div id="outerBox">
        <li>Item A</li>
        <li>Item B</li>
        <li>Item C</li>

The following script will look for the first <li> tag, then seek out the closest ancestor that matches what was specified in the parameter, in this case "div", for .closest().


The result: