jQuery closest() Method

Last Updated:

jQuery closest() Method

The jQuery closest() method returns the closest ancestor (specified element) of the selected element.

Note: Ancestor means a specified element's parent, grandparent, great-grandparents and so on.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> div{ border: 1px solid black; padding: 25px; } </style> </head> <body> <h1>jQuery closest() Method</h1> <div> <div> <p>This is paragraph inside 2 div.</p> </div> </div> <script> $(document).ready(function(){ $("p").closest("div").css("border", "1px solid red"); }); </script> </body> </html>

Syntax

$(selector).closest(elem);

Parameter Values

Value Type Explanation
elem Required Specifies an element to match elements against.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
closest()YesYesYesYesYesYesYesYesYesYesYesYesYes

Share this Page

Meet the Author