tags:

views:

49

answers:

3

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,

+5  A: 

How about:

alert( $(this).closest('div').attr('id') );
VoteyDisciple
@Gully, if that isn't working you are doing some thing else wrong,
mikerobi
A: 
alert ($(this).parent().attr('id'));
Karam89
+2  A: 

Or to be extra efficient and skip jQuery altogether:

alert(this.parentNode.id);
lonesomeday
I'd +1 if I had any more votes left today. Using jQuery for EVERYTHING is really unhealthy.
Coronatus
I've got a +1 for you. This is how I'd do it. *Much* better performance.
patrick dw
@patrick I was sceptical about this method having "much" better performance. so I made a little testbench here http://vidasp.net/tinydemos/jQuery-vs-native.html The results are: jQuery ~190ms, Native ~3ms. Amazing!
Šime Vidas
@Šime Vidas - Yes, it doesn't really stand a chance against two simple and direct property lookups.
patrick dw