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. Syntax:
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.
jQuery code to show the working of closest() method:
In the below code, optional parameter is not passed.
<!--Here among dom id first ancestor will select -->
var item = document.getElementById("dom");
"border": "2px solid green"
This is great-great-grandparent !
This is second ancestor !
This is first ancestor !
<li>This is direct parent !
<span>This is span the child one !</span>
You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
Please Login to comment...