I know I can handle missing image error with jQuery like this:
$('img').on('error', function() {
var $this = $(this);
$this.attr('src', '/image-placeholder.svg');
});
This works just fine on page load. However, I have an Ajax call that returns HTML string to be placed on the page. The HTML also has images in it that might be missing. For the Ajax case the code above does not work: the missing resources are throwing 404 but they are not replaced by the placeholder. I tried to use event delegation, but no luck:
$(document).on('error', 'img', function() {
var $this = $(this);
$this.attr('src', '/image-placeholder.svg');
});
Is there a way to handle the missing resources after they've been returned via Ajax response?
My Ajax call uses the $.get()
method.