Skip to content Skip to sidebar Skip to footer

YUI3: How Would I Trigger An Event?

Suppose I have a click event on a link/button/etc. var myButton = Y.one('.button'); myButton.on('click', function() { // code }); There is something else happening on the page

Solution 1:

If you are looking for equivalent of trigger in yui3 you can try using the 'simulate'

Y.one('button selector').simulate('click');

For the above statement to work you will need to add "node-event-simulate" roll up in the use method.


Solution 2:

Do you really need to trigger the click event on the button? Take the HTML below

    <button id="myButton">Click Me</button>
<br>
    <a href="#">Click Me</a>

You can make use of the custom events to put the real logic somewhere central.

YUI().use("node","event",function(Y){
    Y.one("#myButton").on("click",function(){Y.fire("custom:doThing")});
    Y.all("a").on("click",function(){Y.fire("custom:doThing")});
    Y.on("custom:doThing",function(){console.log("Do my thing, regardless of event source")})
});

Fiddle: http://jsfiddle.net/WZZmR/


Post a Comment for "YUI3: How Would I Trigger An Event?"