JavaScript
Article

jQuery Detect and Hide Broken Images

By Sam Deering

jQuery code snippet to either display a default image for those that are broken or detect and hide all broken images. Hide all those red crosses! ;)

// Replace source
$('img').error(function(){
        $(this).attr('src', 'missing.png');
});

// Or, hide them
$("img").error(function(){
        $(this).hide();
});

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.