Search code examples
eventsclickyui

How to write a common event for all buttons in YUI3?


As question describe all, but just for clarification, I want to do a common stuff like alert
value when i click on any button.

Please suggest a solution in YUI3 only because i am using YUI3.


Solution

  • There is a more efficient way to do it:

    Y.delegate('click', function(e) { ... }, 'body', 'button');
    

    This attaches a single event handler to the body instead of attaching a separate event handler to each button.

    YUI Documentation - delegation