3

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

This will only output the img elements. Any other elements in the htmlData will be lost. Use this instead: var output = $(htmlData); output.filter('img').error(function () {$(this).hide();}); $(<selector>).html(output);. Regardless, I think this is precisely what the OP was trying to avoid.
@JosephSilber Good catch, I added .end() to the end of the jQuery chain to return the the parent selector. The benefit to this method is that it only binds the events to the elements necessary, no bubbling needed which is a performance benefit.
Well, event bubbling occurs regardless of whether you've attached an event handler... Actually, I think in this particular case, live would be better for performance, since you only bind the event handler on 1 element (the document).
jsfiddle.net/3nXcS/3 The error event does not bubble up to the document.
yeahh.. thanks for the demo.. i thought there is an easier to do that just like adding a live() .. but anyways, i appreciated your help.. thanks.
0

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

Try,

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

1 Comment

it still doesn;t work.. i tried putting an alert jsut to know if it triggers, but no alert happen..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.