How can I find a specific ancestor DOM element using jQuery?

0
=
0
+
0
No specific Bitcoin Bounty has been announced by author. Still, anyone could send Bitcoin Tips to those who provide a good answer.
0

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!

1 Answer

1
=
2
=
$0.13
2 tips with total amount of 0.5 mBTC($0.13 USD) have been sent by Anonimous

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>
SEND BITCOIN TIPS
User rating:

Thank You for your help!

Good answer

Привет из России :)

0

Too many commands? Learning new syntax?

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!

Post Answer