This example demonstrates different ways to create a Push Button.
A Push Button can be instantiated three different ways:
<input type="button"/>
or <button type="button"/>
element<input type="button"/>
or <button type="button"/>
elementA Button can be created using an existing <input type="button"/>
or <button type="button"/>
element as a source element, the attributes of which are captured and used for the creation of a new element that replaces the source element inline.
1 | <button type="button" id="pushbutton1" name="button1" value="Add">Add</button> |
2 | <input type="button" id="pushbutton2" name="button2" value="Add"> |
view plain | print | ? |
Pass the id of the source element as the first argument to the Button's constructor. Additional configuration attributes for a Button can be set at instantiation time by specifying them in an object literal that is passed as the second argument to the Button's constructor. Note: the value of configuration attributes passed to the Button constructor will trump those of the corresponding HTML attributes of the original source element.
1 | var oPushButton1 = new YAHOO.widget.Button("pushbutton1"); |
2 | var oPushButton2 = new YAHOO.widget.Button("pushbutton2"); |
view plain | print | ? |
A Button can also be instantiated using pre-defined Button Control HTML: An element with a class of "yui-button" and "yui-push-button" containing a element with a class of "first-child" containing either an <input type="button"/>
or <button type="button"/>
element:
1 | <span id="pushbutton4" class="yui-button yui-push-button"> |
2 | <span class="first-child"> |
3 | <input type="button" name="button4" value="Add"> |
4 | </span> |
5 | </span> |
6 | <span id="pushbutton5" class="yui-button yui-push-button"> |
7 | <em class="first-child"> |
8 | <button type="button" name="button5">Add</button> |
9 | </em> |
10 | </span> |
11 | <span id="pushbutton6" class="yui-button yui-push-button"> |
12 | <strong class="first-child"> |
13 | <button type="button" name="button6">Add</button> |
14 | </strong> |
15 | </span> |
view plain | print | ? |
To instantiate a Button using the Button Control HTML, pass the id of the Button's root element (the element with the classes "yui-button" and "yui-push-button" applied) as the first argument to constructor and any additional configuration attributes as the second argument via an object literal.
1 | var oPushButton4 = new YAHOO.widget.Button("pushbutton4"); |
2 | var oPushButton5 = new YAHOO.widget.Button("pushbutton5"); |
3 | var oPushButton6 = new YAHOO.widget.Button("pushbutton6"); |
view plain | print | ? |
To build a Button without any existing HTML, pass a set of configuration attributes as a single argument to the constructor using an object literal.
1 | var oPushButton7 = new YAHOO.widget.Button({ |
2 | label:"Add", |
3 | id:"pushbutton7", |
4 | container:"pushbuttonsfromjavascript" }); |
view plain | print | ? |
In most cases, it is necessary to specify the button's id, type, label and container (the HTML element that the button should be appended to once created). If an id is not specified for the button, one will be generated using the generateId
method of the Dom utility. Similarly, if the "type" attribute is omitted, the default type of "button" will be applied.
All of the the events for Button (including DOM-based events such as "mouseover" or "click") can be listened for via addListener
method (or on
for short).
Note: To listen for DOM-based events, always use the provided event interface rather than attaching handlers directly to Button or ButtonGroup DOM elements.
The event object is passed to the handler function as the first argument. For DOM events, this is the actual event object.
1 | // "click" event handler for each Button instance |
2 | |
3 | function onButtonClick(p_oEvent) { |
4 | |
5 | YAHOO.log("You clicked button: " + this.get("id"), "info", "example1"); |
6 | |
7 | } |
8 | |
9 | var oPushButton1 = new YAHOO.widget.Button("pushbutton1"); |
10 | oPushButton1.on("click", onButtonClick); |
view plain | print | ? |
It is also possible to add a "click" event handler via the "onclick"
configuration attribute. The "onclick" configuration attribute accepts an
object literal representing the code to be executed when the Button
is clicked. The format for the object literal is:
{
fn: Function (Required), // The handler to call
when the event fires.
obj: Object (Optional),
// An object to pass back to the handler.
scope:
Object (Optional) // The object to use for the scope of
the handler. (By default the scope is the YAHOO.widget.Button instance)
}
1 | var oPushButton2 = new YAHOO.widget.Button("pushbutton2", { onclick: { fn: onButtonClick } }); |
view plain | print | ? |
Add icons to Buttons via CSS. Set the "background" property of the Button's <button/>
element to the url of the icon:
1 | .yui-button#pushbutton2 button, |
2 | .yui-button#pushbutton5 button, |
3 | .yui-button#pushbutton8 button { |
4 | |
5 | background: url(../button/assets/add.gif) center center no-repeat; |
6 | text-indent: -4em; |
7 | overflow: hidden; |
8 | padding: 0 .75em; |
9 | width: 2em; |
10 | *margin-left: 4em; /* IE only */ |
11 | *padding: 0 1.75em; /* IE only */ |
12 | |
13 | } |
14 | |
15 | .yui-button#pushbutton3 button, |
16 | .yui-button#pushbutton6 button, |
17 | .yui-button#pushbutton9 button { |
18 | |
19 | padding-left: 2em; |
20 | background: url(../button/assets/add.gif) 10% 50% no-repeat; |
21 | |
22 | } |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 327ms (+1) 11:37:30 PM:
Button loggerLink
Initialization completed.
INFO 326ms (+0) 11:37:30 PM:
Button null
Setting attribute "href" using source element's attribute value of "btn_example01.html"
INFO 326ms (+1) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 325ms (+0) 11:37:30 PM:
Button pushbutton6
Initialization completed.
INFO 325ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button6"
INFO 325ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 325ms (+0) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 325ms (+1) 11:37:30 PM:
Button pushbutton5
Initialization completed.
INFO 324ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button5"
INFO 324ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 324ms (+0) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 324ms (+1) 11:37:30 PM:
Button pushbutton4
Initialization completed.
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "value" using source element's attribute value of "Add"
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button4"
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 323ms (+0) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 323ms (+0) 11:37:30 PM:
Button pushbutton3
Initialization completed.
INFO 323ms (+0) 11:37:30 PM:
Button null
Source element could not be used as is. Creating a new HTML element for the button.
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "value" using source element's attribute value of "Add"
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button3"
INFO 323ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 323ms (+0) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 323ms (+1) 11:37:30 PM:
Button pushbutton2
Initialization completed.
INFO 322ms (+0) 11:37:30 PM:
Button null
Source element could not be used as is. Creating a new HTML element for the button.
INFO 322ms (+0) 11:37:30 PM:
Button null
Setting attribute "value" using source element's attribute value of "Add"
INFO 322ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button2"
INFO 322ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 322ms (+0) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 322ms (+2) 11:37:30 PM:
Button pushbutton1
Initialization completed.
INFO 320ms (+0) 11:37:30 PM:
Button null
Source element could not be used as is. Creating a new HTML element for the button.
INFO 320ms (+0) 11:37:30 PM:
Button null
Setting attribute "value" using source element's attribute value of "Add"
INFO 320ms (+0) 11:37:30 PM:
Button null
Setting attribute "name" using source element's attribute value of "button1"
INFO 320ms (+0) 11:37:30 PM:
Button null
Setting attribute "type" using source element's attribute value of "button"
INFO 320ms (+23) 11:37:30 PM:
Button null
Building the button using an existing HTML element as a source element.
INFO 297ms (+1) 11:37:30 PM:
LogReader instance0
LogReader initialized
INFO 296ms (+1) 11:37:30 PM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 295ms (+0) 11:37:30 PM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 295ms (+44) 11:37:30 PM:
Get
_next: q0, loaded: undefined
INFO 251ms (+1) 11:37:30 PM:
Button pushbutton9
Initialization completed.
INFO 250ms (+0) 11:37:30 PM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 250ms (+1) 11:37:30 PM:
Button pushbutton8
Initialization completed.
INFO 249ms (+0) 11:37:30 PM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 249ms (+2) 11:37:30 PM:
Button pushbutton7
Initialization completed.
INFO 247ms (+247) 11:37:30 PM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 0ms (+0) 11:37:30 PM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings