javascript : .closest(cssSelector)
javascript : .closest( cssSelector ) Target: Applicable with any DOM element. getElementById getElementsByTagName getElementsByClassName getElementsByName querySelector querySelectorAll select etc. Description : it search for closest parent element that matched with the selector. It traverse parentNode to parentNode untill matched with selector if no any parent element matched then it return empty array of nodeElementsList instead of null so nested statement not generate error due to null reference. Note : it start searching of selector from the element itself. That's mean if the element matches with the selector it return itself. Syntax: DOMElement.closest( cssSelector ) DOMNodeList.closest( cssSelector ) HTMLCollection.closest( cssSelector ) Usase: document.getElementById('firstDiv').closest( 'form' ) it search for firstDiv Element's parent form. document.getElementsByTagName('div').closest( 'form' ) the getElementsByTagName selects all div a...