Javascript Unerror method using another js file

<img src="images/business-card.webp" onerror="this.onerror=null; this.src='images/business-card.jpg' " class="m-auto img-fluid">

I want to write this feature from within js file. How can i do it ?

Answer

First, create the image in javascript:

var image = document.createElement("IMG");
document.body.appendChild(image);
image.src = "images/business-card.webp;
image.class = "m-auto img-fluid";

Now, add an onerror function:

blah.onerror = function(){this.onerror=null; this.src='images/business-card.jpg'};