Last Updated: February 25, 2016
·
840
· rjv

Remove broken placeholders when images don't load

Images that don't load can sometimes leave an ugly red [X] placeholder. This jQuery snippet will remove all instances of images that don't load. It even takes into account that IE will only produce a single error for each unique src attribute.

$('img').error(function(){
    $('img[src="'+$(this).attr('src')+'"]').remove();
});

It's safer to completely remove the broken image from the DOM as the display and visibility CSS properties work inconsistently across browsers.