MeltingDog MeltingDog - 5 months ago 9
jQuery Question

Difficulty hiding image with no src

I have a series of the following elements:

<div class="slidetpl">
<img src >
</div>


I want to hide .slidetpl if the image has no source. To do that I have tried the following:

$('.slidetpl img').each(function () {
if (this.src.length == 0) {
$(this).parent().hide();
}
});


But this is not working.

Can anyone point me in the right direction or tell me what I've done wrong?

Answer

Try this:

 $(document).ready(function(){
    $('.slidetpl img[src=""]').parent().hide();
    $('.slidetpl img:not([src=""])').parent().show(); //Can be removed.
});
Comments