The closest()
method is a built-in method in jQuery. This method looks for the first ancestor of the element by traversing upward from the current element. A successful search returns the element itself; otherwise, a null is returned.
$(selector).closest([selector])
More about selectors can be found here.
In the code below, the closest()
method will start searching for the first ancestor from the span element and change its border color from black to red.
Free Resources