Sometimes, we want to handle HTML image not found with JavaScript.
In this article, we’ll look at how to handle HTML image not found with JavaScript.
How to handle HTML image not found with JavaScript?
To handle HTML image not found with JavaScript, we set the onerror
attributes.
For instance, we write
<img
id="currentPhoto"
src="SomeImage.jpg"
onerror="this.onerror = null; this.remove();"
alt="2"
width="100"
height="120"
/>
to call this.remove
in the onerror
attribute value to remove the img element if the image can’t load.
Conclusion
To handle HTML image not found with JavaScript, we set the onerror
attributes.