Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove future broken image using jquery

Tags:

jquery

will normally, this will work..

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

but it doesnt work when putting a live on it

$("img").live("error",function(){$(this).hide();});

the problem is that, for those image which are ajax generated, i cannot hide the broken image.

like image 731
lidongghon Avatar asked Jun 03 '26 10:06

lidongghon


1 Answers

You could add the event handler as you add the images to the DOM:

$.get(urlHere, function(htmlData) {
    var output = $(htmlData).find('img').error(function () {$(this).hide();}).end();
    $(<selector>).html(output);
});

Here is a demo: http://jsfiddle.net/3nXcS/2/

Update

When you console.log() the e.bubbles variable it returns false. So you can't use a binding method that requires bubbling (.delegate(), .live()).

like image 158
Jasper Avatar answered Jun 05 '26 22:06

Jasper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!