var selector = '';
$.each(data, function(key, val) {
var img = '/wp-content/img/history/' + val.image_id + '.jpg';
selector = '#img'+(key+1);
$(selector).prepend('<img id src=' + img + ' alt="pic" />');
});
The above code snippet works, the images are loaded into the elements #img1, #img2, #img3, #img4. I cannot be sure the images exist so a .done/.fail was added to the file get.
var selector = '';
$.each(data, function(key, val) {
var img = '/wp-content/img/history/' + val.image_id + '.jpg';
selector = '#img' + (key + 1);
$(selector).removeClass('d-none');
$.get(img).done(function() {
$(selector).empty();
$(selector).prepend('<img id src=' + img + ' alt="pic" />');
}).fail(function() {
alert(val.image_id + ' not found');
});
});
After done/.fail on the file get are added 'key' is no longer being incremented. Using a counter within the loop behaves the same way as 'Key'.
Does anyone have an insight as to the cause?