I need to find a specific ancestor element (parent, grandparent, etc) which MAY be located FEW LEVELS above the given (child / descendant) element in the HTML DOM tree. The specific element I am looking for is marked with a special CLASS, i.e. .
> ... class = "parent" ...
I'd like to use jQuery to locate that ancestor element starting from one of its descendant elements. Thanks!
I've found an answer.
Need to use function ".closest". For example:
<div class="parent">
<div>
<div class="child"></div>
</div>
</div>
<script> var oParent = $('.child').closest('.parent'); </script>
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!