20
I have an HTML page with numerous images presented as follows:
<img alt="img1" width="50" height="120" src="my_image.jpg" />
<img alt="img2" width="50" height="120" src="another_image.jpg" />
<img alt="img3" width="50" height="120" src="yet_another_image.jpg" />
If any of the images do not load due to reason A or B, there will be breaks in the layout or some browsers, the image square will appear with the alternate text, making the whole look a little ugly.
Question
How can I via jQuery add a CSS class to images whose load action failed ?
Example:
<img alt="img1" width="50" height="120" src="my_image.jpg" class="failToLoad" />
If the image is external (e.g. Imgur) with an invalid url the image will not generate error 404, so solutions using the event
error
may not cover all cases.– BrunoLM