0

I'm trying to use a script to change the visibility of a div if an image inside it is missing

here is the div:

 <div id="bike01">
<a href="stock/bike1.html">
<table width="900" border="0" cellpadding="3" cellspacing="0" class="table">
<tr>
  <td width="245" rowspan="2"><img id="bike1" src="stock/bike1/images/image1.jpg" name="bike1" width="245" height="184" />
  </td>
  <td width="468"><div id="title1"></div></td>
</tr>
<tr>
  <td colspan="2"><div id="desc1"></div></td>
</tr>
</table>
</a>
</div>

and here is my script (I am a complete beginner when it comes to scripting - ive just picked this up from researching my question)

 <script>
$("#bike1").error(function () { 
$("#bike01").css({visibility:"hidden"});  
});
</script>

Basically if the img#bike1 is missing i want the whole div #bike01 to disappear.

Stephen
  • 7
  • 1
  • 1
    possible duplicate of [How do I hide broken images in javascript?](http://stackoverflow.com/questions/3675073/how-do-i-hide-broken-images-in-javascript) – sdgluck Aug 17 '15 at 20:00

2 Answers2

1

Why not do:

<img id="bike1" src="stock/bike1/images/image1.jpg" onerror="this.style.display='none';" />

How do I hide broken images in javascript?

<img id="bike1" src="stock/bike1/images/image1.jpg" onerror='$("#bike01").css({visibility:"hidden"});' />
Community
  • 1
  • 1
Stephen King
  • 816
  • 4
  • 14
0

Try this:

<script>
    $("#bike1").on('error', function() {
        $("#bike01").hide();
    });
</script>
Sushil
  • 2,837
  • 4
  • 21
  • 29
DieVeenman
  • 457
  • 1
  • 3
  • 18