user3086509
user3086509

Reputation: 135

Can't get .click() and similar Events working properly

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

Answers (2)

aksu
aksu

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

Snehal S
Snehal S

Reputation: 875

try this,

$(document).on('click', '.pic', function() {
    //do your stuff
});

Upvotes: 2

Related Questions