provola provola - 1 month ago 10
Javascript Question

Jquery .error method replacement

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

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


This code does'nt work because .error has been removed in jquery 3.x.
What Jquery method or Javascript method can I use to test for broken image?

Answer

Use on:

$('img').on("error", function(){
        $(this).attr('src', 'missing.png');
});

See the jQuery 3 migration guide.

Comments