0

I'm generating images from a folder with this script:

$(document).ready(function() {
$.ajax({
    url: "gallery_images",
    success: function(data){
        $(data).find("a:contains(.jpg),a:contains(.gif),a:contains(.png)").each(function(){
            // will loop through
            var images = $(this).attr("href");

            $('<div class="g_image"></div>').html('<img class="g_img" src="gallery_images/'+images+'"/>').appendTo('#galerija');
        });
    }
});

});

The problem is that, then I'm trying to click the image, simple jQuery click event does not work.

$(".g_image img").click(function(){
alert("WORKING!");

});

linasb
  • 13
  • 1

2 Answers2

5

Use event delegation for this, you can take closest parent document instead document.body

$(document.body).on("click","#g_image img",function(){
    alert("WORKING!");
});
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
2

try using delegate.

$("body").on("click","#g_image img",function(){
 alert("WORKING!");

});
user1
  • 1,063
  • 1
  • 8
  • 28