eculver
eculver

Reputation: 517

Is it possible to do live events in YUI, similar to jQuery's live events?

It doesn't currently seem as though there is a way to do live events in YUI similar to jQuery: http://api.jquery.com/live/

It sure would be nice if something like:

function handleClick(e) {
    // click!
}

YUI().use('node-base', function(Y) {
    Y.on("click", handleClick, ".foo");
});

caused handleClick to be fired when a node with the "foo" class was clicked on after being dynamically added to the DOM and sometime after the Y.on was evaluated. I feel like live events are one of jQuery's killer features that YUI lacks.

Upvotes: 1

Views: 565

Answers (2)

Walter Rumsby
Walter Rumsby

Reputation: 7535

YUI 2's Event Utility also supports a delegate method.

Upvotes: 1

Tivac
Tivac

Reputation: 2573

You can use YUI3's support for event delegation to accomplish the same thing.

YUI().use("node", "event", function(Y) {
    Y.delegate("click", function() {
        //click!
    }, Y.config.win, ".foo");
});

I think attaching that to the window is ok, you might need to attach it to the body instead.

Use Y.one("body") instead of Y.config.win in that case.

Upvotes: 1

Related Questions