Is there a way to detect when a dynamically created img src path returns an error without using the onerror property in the HTML syntax, but using just a JavaScript file?
My HTML:
<img class="icon" src="/test.png" alt="Logo">
My JS, using JQuery 3.3.1:
$(document).ready(function() {
$(".icon").onerror = function() {
$(this).attr('src', '/res/default-icon.jpg');
};
});
Or also tried like this:
$(document).ready(function() {
$(document).on("error", ".icon", function() {
$(this).attr('src', '/res/default-icon.jpg');
});
});
But it doesn't work. Thanks for the help.