jQuery closest() Method
jQuery closest() Method
The closest() method is used to return the first ancestor of the selected element. An ancestor can be a parent, grandparent, great-grandparent, and so on. In this method, traversing happens upwards from the current element. It is similar to parents() method.
Some of its properties are:-
- It begins with the current element
- Travels up the DOM tree and returns the first ancestor that matches the passed expression
- The returned object contains zero or one element
Syntax:-
Its syntax is:- $(selector).closest(filter, context)
Further Explanation:-
Parameter | Description |
---|---|
filter | Required. Specifies a selector expression, element, or jQuery object. |
context | Optional. An element within which a matching element may be found |