Reputation: 8597
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!
Upvotes: 1
Views: 111
Reputation: 9723
Try this:
$('.searchimg').on('click',function(){
alert($(this).parent().next().children('.name-info').attr("id"));
});
Upvotes: 0
Reputation: 388416
This should do
$('.search img').on('click',function(e){
alert($(this).parent().next().find('.name-info').attr('id'))
});
Demo: Fiddle
Since you are clicking the img
element, you need to get the next slibling
of the parent, then find the element with class name-info
.
Upvotes: 1
Reputation: 191789
$(".span2 img").on('click', function () {
console.log($(this).parent().next().find('.name-info').attr('id'));
});
Upvotes: 2
Reputation: 960
I think $('.searchimg') should be $('.search img') with what you're trying to do...
Upvotes: 0