jQuery | closest() with Examples
The closest() is an inbuilt method in jQuery that returns the first ancestor of the selected element in the DOM tree. This method traverse upwards from the current element in the search of first ancestor of the element. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Parameter: It accepts two parameters which are specified below-
- para1: This specifies the element to narrate the ancestor search in the DOM tree.
- para2: This is an optional parameter DOM element within which a matching element being found.
Return Value: It returns the first ancestor for the selected element.
In the below code, optional parameter is not passed.
In the below code, optional parameter is passed to the method.
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.