I'm trying to trigger a click event on a dynamically created element. I have used .on() to bind the click events, but it seems that the trigger method is firing before the element is created. I have tried triggering the click in several locations and nothing seems to work. Any help would be appreciated. I'm trying to trigger a click on the first span that is created using $('container:first-child').trigger('click');
displayThumbnails = function() {
$(photoArray).each(function(index) {
var thumbnailImg = this.SmallImageUrl,
largeImg = this.LargeImageUrl,
thumbnailId = 'thumbnail' + index,
largeImgId = 'image' + index;
if(index > indexCount) {
return false;
} else if (index >= indexStartValue && index <= indexCount) {
$(thumbContainer).append(
'<span class="thumbnail" id="' + thumbnailId + '"><img height="45px" width="60px" src="' + thumbnailImg + '"/></span>'
);
$(thumbContainer).on('click', '#' + thumbnailId, function(){
$(thumbContainer).find('.active').removeClass('active');
$(this).addClass('active');
$('#displayed-image').attr('src', largeImg);
});
}
});
};