javascript - Click event on dynamicaly added images -
this question has answer here:
i'm generating images folder script:
$(document).ready(function() { $.ajax({ url: "gallery_images", success: function(data){ $(data).find("a:contains(.jpg),a:contains(.gif),a:contains(.png)").each(function(){ // 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 that, i'm trying click image, simple jquery click event not work.
$(".g_image img").click(function(){ alert("working!"); });
use event delegation this, can take closest parent document instead document.body
$(document.body).on("click","#g_image img",function(){ alert("working!"); });
Comments
Post a Comment