I am trying to get the closest/nearest class of a specific element.
How can I make it work? I am currently trying to use .closest.
HTML
<div class="test">.test</div> <!-- Not this -->
<div>
<div class="test">.test</div> <!-- That's the one I need to grab -->
</div>
<div id="test"></div> <!-- This is the reference element -->
<div class="another"></div>
<div class="another"></div>
<div class="test">.test</div> <!-- Not this -->
JS
var test = $('#test2').closest('.test');
console.log(test);
If closest isn't the right method, how can I actually grab the closest 'test' class?
I tried parentsUntil() as well.
Actually if that can help, in my use case I will ALWAYS need the nearest PARENT element of a specific class, whether that parent element is within another element or not, like in this HTML example.
Aucun commentaire:
Enregistrer un commentaire