How to remove img tag whose img not loaded | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to remove img tag whose img not loaded

I have two img tag. One with invalid src and other with valid src. Invalid one decreses in height if image don't load. I want to target such images from stack whose images not loaded and show alt text and set them to display none using javascript. Here is my try but not working well. Plz suggest any idea 💡 if you have?? https://code.sololearn.com/WNCjemUgMI8F/?ref=app

26th Sep 2020, 2:24 AM
Divya Mohan
Divya Mohan - avatar
5 Answers
+ 1
https://code.sololearn.com/WNCjemUgMI8F/?ref=app
26th Sep 2020, 2:29 AM
Divya Mohan
Divya Mohan - avatar
+ 1
SoundInfinity I use it as img.onerror = ()=>{ img.style.display = "none" } But 😒 it didn't work
26th Sep 2020, 2:37 AM
Divya Mohan
Divya Mohan - avatar
0
You didn't even post ur try, but okay. Anyways, you can use 'img.onerror' which I think fires when the image fails to load.
26th Sep 2020, 2:25 AM
SoundInfinity
SoundInfinity - avatar
0
I'm pretty sure the image must be reloaded after assigning that event, so it can fire it.
26th Sep 2020, 2:42 AM
SoundInfinity
SoundInfinity - avatar
0
I think this might work. Add the attribute, "onerror", directly to the image element, like so: <img onerror='this.remove()'></img> That will remove the image in case it fails to load.
26th Sep 2020, 2:52 AM
SoundInfinity
SoundInfinity - avatar