Get parent id from jquery div

I have an image inside a div, when I click on the image, I want it to warn me about the parent id, which will be "group1"

<div id="group1">
<img class="header_logo_dis" src="test.png">
</div>

$('.header_logo_dis').click(function() {
    alert($(this).parent("div:first"));
});

Thank,

+3
source share
3 answers

What about:

alert( $(this).closest('div').attr('id') );
+21
source

Or be more efficient and skip jQuery in general:

alert(this.parentNode.id);
+20
source
alert ($(this).parent().attr('id'));
+1
source

Source: https://habr.com/ru/post/1772258/


All Articles