jhloke
jhloke

Reputation: 155

jQuery .delegate() not working on load and changeData events

Can someone enlighten me on the jQuery delegate, what events are handled and what aren't. The follow code doesn't work

$("#browser").delegate( ".photo", {
    "load": function(e) {
        alert("photo loaded");
    }
});

but the following code work

$(".photo").load( function(e) {
    alert("photo loaded");
} );

I also try to delegate changeData event to a class which doesn't work as well

$("#browser").delegate( ".thumbnail", {
    "changeData": function(e, prop, value) {
        alert( prop + " = " + value );
    }
});

but the following code work

$(".thumbnail").bind( "changeData", function(e, prop, value) {
    alert( prop + " = " + value );
}

Upvotes: 5

Views: 12722

Answers (3)

yunzen
yunzen

Reputation: 33449

For newer versions of jQuery, where the .on() method is favored before the .delegate() or .live() methods, it won't work in this way, too

// wrong
jQuery(selector).on('load', 'img', myFunc)

because the load event does not bubble

make it like this

jQuery(selector).find('img').on('load', myFunc)

Upvotes: 0

Aidiakapi
Aidiakapi

Reputation: 6259

Not:

$("#browser").delegate( ".photo", {
    "load": function(e) {
        alert("photo loaded");
    }
});

But:

$("#browser").delegate( ".photo", "load",
    function(e) {
        alert("photo loaded");
});

And you cannot use live and delegate with those events, because they don't bubble.

Upvotes: 11

SLaks
SLaks

Reputation: 888283

These events do not bubble, so they cannot be used with live or delegate.

Upvotes: 6

Related Questions