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

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

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 - total earned - 0.5 mBTC ($0.13 USD)

2 tips with total amount of 0.5 mBTC($0.13 USD) have been sent by Non-Registered User

I've found an answer.

Need to use function ".closest". For example:

<div class="parent">
      <div class="child"></div>
<script> var oParent =  $('.child').closest('.parent'); </script>

Thank You for your help!

Good answer

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


Post Answer

Will Bitcoin Tipping become a new Internet Culture?

Please share our story! THANK YOU!