Home » Categories » JavaScript

How to hide image broken Icon using only CSS/HTML?

<img src="Error.src" onerror="this.style.display='none'"/>

or
<img src="Error.src" onerror="this.src='fallback-img.jpg'"/>

 

document.addEventListener("DOMContentLoaded", function(event) {
   document.querySelectorAll('img').forEach(function(img){
  	img.onerror = function(){this.style.display='none';};
   })
});

<img src="error.src">
<img src="error.src">
<img src="error.src">
<img src="error.src">


Reference:
https://stackoverflow.com/questions/22051573/how-to-hide-image-broken-icon-using-only-css-html
 
Article Rating (No Votes)
Rate this article
  • Icon PDFExport to PDF
  • Icon MS-WordExport to MS Word
 
Attachments Attachments
There are no attachments for this article.
Comments Comments
There are no comments for this article. Be the first to post a comment.
Related Articles RSS Feed
XMLHttpRequest.responseURL
Viewed 557 times since Wed, Sep 30, 2020
Get The Current Domain Name With Javascript (Not the path, etc.)
Viewed 631 times since Fri, Oct 2, 2020
Hidden image after redirect by check filename
Viewed 673 times since Wed, Sep 30, 2020
Regex in Javascript to remove links
Viewed 576 times since Sat, Oct 3, 2020
Strip HTML Tags in JavaScript
Viewed 602 times since Sat, Oct 3, 2020
Check image width and height before upload with Javascript
Viewed 580 times since Mon, Sep 21, 2020