8

i have a div with the class 'userFeatureProductImage'. this div is repeated however occasionally a broken image might come through so i want to hide the div as a precaution if this does happen. how can i do this?

0

4 Answers 4

4

The load event is only fired if an image is valid:

$('.userFeatureProductImage').hide();
$('.userFeatureProductImage img').load(function() {
  $(this).closest('.userFeatureProductImage').show();
});
Sign up to request clarification or add additional context in comments.

Comments

2

Use the error event which is called when an error occurs with the image:

$(".userFeatureProductImage img").error(function(){
   $(this).parent().hide();
});

Example: http://jsfiddle.net/jonathon/vWwpc/

6 Comments

This doesn't seem to work for me... jsfiddle.net/UzEKZ/1 ; The documentation makes it sound like it only hides the broken image icon for IE users.
hi this doesnt work for me either. just wondering would this just hide the image instead of the parent div?
hi got it to work, $(".userFeatureProductImage img").error(function () { $(this).parent().hide(); });
Hi - not entirely certain why this is the case. I've added a working example, but for some reason it doesn't work correctly when I have it in the 'JavaScript' box. Possible because the event is raised before. I've also updated it to hide the parent div.
Ahh - it wasn't working in jsFiddle but when I changed the wrap to onDomReady, it worked.
|
2

You might try something like this:

<script type="text/javascript" 
        src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
     function hideBrokenImage(id) {
          $(id.+"userFeatureProductImage").hide();
     }
</script>
<div id="imageFilename" class="userFeatureProductImage">
     <img src="imageFilename.gif" onerror="hideBrokenImage(imageFilename);" />
</div>

Where imageFilename is obviously dynamically created.

Comments

0

jQuery equivalent of PHP's file_exists()?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.