What is the jQuery closest() method?

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.

Syntax

$(selector).closest([selector])

More about selectors can be found here.

Code

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

Copyright ©2025 Educative, Inc. All rights reserved