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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved