I have something like this:
<div class='span6'>
<div class='row-fluid'>
<div class='span2 search'>
<img src='img.png'/> <!-- when clicked -->
</div>
<div class='span10'>
<div class='name-info' id="test"> <!-- get this id -->
<h4 class="name"><?php echo $name;?></h4>
<div class='address'>
<?php echo $location;?>
</div>
</div> <!-- end name-info -->
</div>
</div> <!-- end name-block -->
</div>
<div class='span6'>
<div class='row-fluid'>
<div class='span2 search'>
<img src='img.png'/> <!-- when clicked -->
</div>
<div class='span10'>
<div class='name-info' id="test"> <!-- get this id -->
<h4 class="name"><?php echo $name;?></h4>
<div class='address'>
<?php echo $location;?>
</div>
</div> <!-- end name-info -->
</div>
</div> <!-- end name-block -->
</div>
$('.search img').on('click',function(){
alert($(this).next('.name-info').attr("id"));
});
I have two exact divs and I’m trying to click on an img, and when I click on the image, it should be able to get the id of the class name-info, which way do I do this? I tried a few functions but not familiar with all jQuery functions. Need help please?
Thanks!
This should do
Demo: Fiddle
Since you are clicking the
imgelement, you need to get thenext sliblingof the parent, then find the element with classname-info.