Rickstar
Rickstar

Reputation: 6199

Get parent ID from div jquery

I have a image inside a div when i click on the image i want it to alert me of the parent id that 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 you,

Upvotes: 3

Views: 28416

Answers (3)

lonesomeday
lonesomeday

Reputation: 238045

Or to be extra efficient and skip jQuery altogether:

alert(this.parentNode.id);

Upvotes: 19

Karam89
Karam89

Reputation: 61

alert ($(this).parent().attr('id'));

Upvotes: 1

VoteyDisciple
VoteyDisciple

Reputation: 37813

How about:

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

Upvotes: 21

Related Questions