Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide broken images jQuery

I have content with broken images, multiple images in each page. Some images have empty src value and some just broken 404 links.

I have been trying to use

<script type="text/javascript">
$(document).ready(function () {
    $("img").error(function(){
    $(this).hide();
    });
}); 
</script>

It doesn't work as expected, doesn't work in IE, and in chrome I have to reload the page after first load to hide the images. Googled a lot, but all other codes are the same.

Editing the <img> tag is NOT an option for me.

What is wrong with that code?

like image 888
D_Guy13 Avatar asked Jul 29 '13 17:07

D_Guy13


1 Answers

Why not just combine DOM events with jQuery:

$("img").each(function () {
    var $this = $(this);

    this.onerror = function() {
        $this.hide();
    };
});

This worked for me.

like image 52
Jordan Avatar answered Sep 22 '22 11:09

Jordan