closest()
will get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. But I want to know if there is any way to get the result excluding itself (I mean just get the parents of it).
Let me take a example. Please review it.
<div id='div1' class="container">
<div id="div2" class="container">
<!--many nested div.container as it directly or indirectly children -->
</div>
<div id="div3" class="container">
<!--many nested div.container as it directly or indirectly children -->
</div>
</div>
$(function() {
$('div.container').on('mouseover', function(e){
//I only want to select the first parent with the class container.
//closest include the this element.
console.log($(this).closest('.container').attr('id'));
});
});
.parents()
will help.