YUI3 - Handling Onclick events for links that have the same classes

Problem: YUI3 - Onclick event handling for links with the same classes

We have several links on a page with the same class. When I click on one of the links, there are several different actions that should be performed based on the link that the link was clicked on, for example,

<a href="?page=1" data="test1" class="sample">One</a>
<a href="?page=2" data="test2" class="sample">two</a>
<a href="?page=3" data="test3" class="sample">three</a>

handler code:

Y.all('.sample').on('click', function(e){
    e.preventDefault();
    alert(this.getAttribute("data"));
});

when I click any of the links, I get a list of all the "data" attributes. We need only these links.

+5
source share
3 answers

all . each node. . Node API .

jsfiddle.

Y.all('.sample').each(function(node) {
    node.on('click', function(e) {
        e.preventDefault();
        alert(node.getAttribute("data"));
    });
});​

Event yuilibrary.com(http://yuilibrary.com/yui/docs/event/#nodelist-this). .

+1

e.target this :

Y.all('.sample').on('click', function(e){
  e.preventDefault();
  alert(e.target.getAttribute("data"));
});

:

Y.one('body').delegate('click', function(e){
  e.preventDefault();
  alert(this.getAttribute("data"));
}, '.sample');
+2

- , @juandopazo

, Y.all('. sample'). each (...) ( @user322896) e.target( @juandopazo), -:

Y.on('click', function(e){
    e.preventDefault();
    alert(this.getAttribute("data"));
}, '.sample');

( directelly), NodeList, this node. Y.all('.sample'), : Y.on() Y.delegate() node.on() node.delegate()?

0

All Articles