I have a situation in which i need a reference to the very next DIV that is the parent of my current DIV, but matches a specific class name.
<div class="container">
<div class="something">test</div>
<div class="something">test</div>
<div class="something" onclick="$(use jquery to find parent with class container)">test</div>
</div>
This would be pretty easy, but it can also look like this:
<div class="container">
<div class="someotherthing">
<div class="something">test</div>
<div class="something">test</div>
<div class="something" onclick="$(use jquery to find parent with class container)">test</div>
</div>
</div>
Which means that i am looking for a solution that recursively walks UP the dom until it finds a parent of a given element that matches a certain CSS class.
And i was wondering whether jQuery can do this in a one-shot call.
The .closest()
method should handle this:
$(this).closest(".someclass")
Take a look at jQuery closest method which get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With