lidongghon
lidongghon

Reputation: 323

How to remove future broken image using 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.

Upvotes: 3

Views: 496

Answers (2)

Jasper
Jasper

Reputation: 76003

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()).

Upvotes: 1

Jim Jose
Jim Jose

Reputation: 1319

The $("img").live method is supposed to work, if not you can try livequery(Livequery)

Try,

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

Upvotes: 0

Related Questions