Reputation: 135
I am heavily stuck at most simple jQuery
stuff...
<div id="wrap">
<div class="pic"><img /></div>
<div class="pic"><img /></div>
...
</div>
I am filling the #wrap
with various images from JSON data,
however, I can't get clicks recognized on by .pic
$(".pic").click(); $(".pic").on("click", function(){}); $(".pic > img").click(); $("img").click
For testing I just add simple alerts to the click function and I tried various combinations without any success.
My goal is to feed the ID of the clicked image back to my JavaScript
.
What am i doing wrong here?
Upvotes: 0
Views: 393
Reputation: 5235
You need to set additional parameter to the .on()
function to get it respond, if the elements are appended dynamically.
$(document).ready(function() {
$("#wrap").on("click", ".pic", function(){
// code
});
});
Upvotes: 1
Reputation: 875
try this,
$(document).on('click', '.pic', function() {
//do your stuff
});
Upvotes: 2