Reputation: 1304
I am working on a CakePHP project using jQuery and AJAX. I have a div which is repeated in a php loop.
This is what's the div look like:
<div class="heart">
<input type="hidden" id="id of loop iterate" />
</div>
What I want is each time the div is clicked I get the id of the input hidden.
What I tried:
jQuery('.heart').click(function(){
var input=$(this).html();
alert(input.prop('id'));
});
Upvotes: 2
Views: 62
Reputation: 346
I have see your code , your code come for dynamic for loop so , simple jquery not work, you want to little change in jquery , have a look here
<script>
$(document).on('click','.heart',function(){
var input = $(this).children().attr('id');
alert(input);
});
</script>
and your html code here , write some thing to find out div like that
<div class="heart">
gjdfjgdfsk
<input type="hidden" id="id of loop iterate" />
</div>
Upvotes: 0
Reputation: 6264
You can use this
$(this).find('input[type="hidden"]').attr('id')
Working Demo
jQuery('.heart').click(function() {
var input = $(this).find('input[type="hidden"]').attr('id')
alert(input);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="heart">
<input type="hidden" id="id of loop iterate" />Click Me !!!!!!!!!!
</div>
Upvotes: 3