Ishmael
Ishmael

Reputation: 32530

How do I get the DOM node within a YUI event handler?

Given a generic handler bound to a series of links with YUI, how do I find out which link triggered the event?

YUI().use('node', function (Y) {
    var list = Y.one('#studentList'), links;
    links = list.all('a');
    links.on('click', function (e) {
        alert(this.get('id')); // this just shows a comma delimited list of all ids
    });
});

I suppose I could bind each link individually instead of using the "on" idiom on the links list, but it seems odd to me that YUI would not provide access to the DOM node. Digging into the event object shows several private fields that look like the DOM node, but surely there must be a safe way of doing this.

Upvotes: 0

Views: 393

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123453

e.currentTarget appears to be what you're looking for:

links.on('click', function (e) {
    alert(e.currentTarget.get('id'));
});

From NodeList's on:

By default, the this object will be the NodeList that the subscription came from, not the Node that received the event. Use e.currentTarget to refer to the Node.

Upvotes: 1

Related Questions