anson920520 anson920520 - 6 months ago 14
jQuery Question

Javascript : Check <img> is empty generate by for loop and if true do something

For instance we have something like below, which is generate brunch of

img
with different src :

//the for loop is not important but just giving you some idea

for(i=0;i<100;i++){
<img class="emptyPic result-image" src="{{picURL}}"/>
}


and I'd like to check every
img
onload, if it's src is empty like
<img src=""/>
, i would like to do something, but in the example just make it
display:none
first. So i tried something like below with JQuery.

if($(this + ".result-image").attr('src')){
($(this + ".result-image").css("display","none")
}


i had put the if statmenet above into the for loop already and it should do the job but what is it not working?

Answer

You can use pure CSS:

img[src = ''] {display:none;}