button.mustache revision e9738690feeb7eae0899f34e5d0feec68bdbd48c
693d70f96fc2b3c1830580edcc29146afd6a9f61Mark Andrews<div class="intro">
<p>In this example, we'll look at a few ways to create buttons using the `'button'` module, which combines `'button-core'`, `'widget'`, and uses and `'cssbutton'` for styling.</p>
<p>There are two ways to create a Y.Button instance. The most common method is to assign it to an existing DOM node, like:</p>
var button = new Y.Button({
var button = new Y.Button(); // Dynamically create the node
Y.one('#container').append(node); // Append it to #container
<p>In either case, the object you receive from the constructor will be a Y.Button instance that contains a few attributes, and wraps around a Y.Node instance. The markup generated will look something similar to:</p>
<p>At this point, you can modify `button`'s attributes, manipulate its state, or listen for change events.</p>
var node = button.getNode()
node.on('click', function(){
<p>Most commonly with button elements, you want to listen for any clicks, or other <a href='/yui/docs/api/files/node_js_node-event.js.html#l8'>DOM events</a> that occur.</p>
var button = new Y.Button({
var node = button.getNode();
node.on('click', function(){
<p>If you are more interested in listening to a `button`'s attribute change events, that can be done at creation.</p>
var button = new Y.Button({
Y.log('This will fire when the toggled state changed')
<p>Any event handlers can also be assigned by using the `on` property in the configuration object. Valid events include any , as well as Y.Button attribute change events (e.g. 'selectedChange').</p>
<p>YUI's Button component was designed with the idea in mind that you may only want button styles, no JS functionality. Instead of `use('button')`, you can just include the `cssbutton` sub-module. This can be done dynamically with `use('cssbutton')`, or statically with a `<link>` tag.</p>
<p>These styles are designed to progressively enhance. In legacy browsers, you'll get styles that appear a bit nicer than native buttons, and in modern browsers you'll get buttons using the latest CSS3 styles.</p>
<p>Here are the events emitted from a Y.Button instance </p>
<p>`button.on('eventName', fn)` will fire <strong>before</strong> the attribute & UI are updated.
<br>`button.after('eventName', fn)` will fire <strong>after</strong> the attribute & UI are updated.</p>