button.mustache revision e9738690feeb7eae0899f34e5d0feec68bdbd48c
d6c63bd07b2e1b034b608fb8af3ae94e6c03ee08Derek Gathright<div class="intro">
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright <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>
800a6a9307a3bbe026f640d20ef73c3d627efc37Derek Gathright{{>getting-started}}
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright<h2>Using Button</h2>
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright<h3>Quick Start</h3>
800a6a9307a3bbe026f640d20ef73c3d627efc37Derek Gathright<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>
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright// Use an existing node
e2f644c80c2428330032af20658279d458938f20Derek Gathrightvar button = new Y.Button({
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright srcNode: '#myButton'
e2f644c80c2428330032af20658279d458938f20Derek GathrightOr, you can also create an unattached node and append to a parent container:
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathrightvar button = new Y.Button(); // Dynamically create the node
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathrightvar node = button.getNode(); // Get the Y.Node instance
e2f644c80c2428330032af20658279d458938f20Derek GathrightY.one('#container').append(node); // Append it to #container
e2f644c80c2428330032af20658279d458938f20Derek Gathright<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>
e9738690feeb7eae0899f34e5d0feec68bdbd48cDerek Gathright<button id='myButton' class='yui3-button' aria-pressed='false'>My Button</button>
<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>