I know this is such a lame question to ask, but it seems like I’m having trouble getting my brain back in gear after the Christmas period.
Simple question, I’m using the jQuery .error event to replace broken images using this bit of script
$('#slideshowContainer img[name=imgStockItem]').error(function(){
$(this).attr('src', '/assets/images/no-image-large.png');
});
What I really want to do is have something like an if/else deal, where if the image is broken do this, else, do this.
I tried this:
if($('#slideshowContainer img[name=imgStockItem]').error()){
alert("true");
} else {
alert("false");
}
But that just returns a true everytime, broken image or not.
Any ideas please?
The
.error()event means.. error.To know if image was loaded successfully, handle the
.loadevent.Now comes the tricky part.. to have jQuery (or JavaScript in general) trigger any of those events, you’ll have to manually assign the image
srcotherwise it will be “too late”.So, have such image tag:
Then such code:
StatusDivis just example for container showing the status of each image.Live test case.