Yahoo! Developer Network Home - Help

YDN Site Search

# YUI Library Examples: Button Control (beta): Push Buttons

# Button Control (beta): Push Buttons

This example demonstrates different ways to create a Push Button.

View example in new window.

Push Buttons
From Markup
From JavaScript

## Creating Push Buttons

A Push Button can be instantiated three different ways:

- Using an existing `<input type="button"/>` or `<button type="button"/>` element
- Using pre-defined Button Control HTML
- Using no existing HTML

### Using an existing `<input type="button"/>` or `<button type="button"/>` element

A 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.

<button type="button" id="pushbutton1" name="button1" value="Add">Add</button> <input type="button" id="pushbutton2" name="button2" value="Add">

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.

var oPushButton1 = new YAHOO.widget.Button("pushbutton1"); var oPushButton2 = new YAHOO.widget.Button("pushbutton2");

### Using pre-defined Button Control HTML

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:

<span id="pushbutton4" class="yui-button yui-push-button"> <span class="first-child"> <input type="button" name="button4" value="Add"> </span> </span> <span id="pushbutton5" class="yui-button yui-push-button"> <em class="first-child"> <button type="button" name="button5">Add</button> </em> </span> <span id="pushbutton6" class="yui-button yui-push-button"> <strong class="first-child"> <button type="button" name="button6">Add</button> </strong> </span>

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.

var oPushButton4 = new YAHOO.widget.Button("pushbutton4"); var oPushButton5 = new YAHOO.widget.Button("pushbutton5"); var oPushButton6 = new YAHOO.widget.Button("pushbutton6");

### Using no existing HTML

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.

var oPushButton7 = new YAHOO.widget.Button({ label:"Add", id:"pushbutton7", container:"pushbuttonsfromjavascript" });

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.

### Adding a "click" event handler to a Button

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.

// "click" event handler for each Button instance function onButtonClick(p_oEvent) { YAHOO.log("You clicked button: " + this.get("id"), "info", "example1"); } var oPushButton1 = new YAHOO.widget.Button("pushbutton1"); oPushButton1.on("click", onButtonClick);

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)
}`

var oPushButton2 = new YAHOO.widget.Button("pushbutton2", { onclick: { fn: onButtonClick } });

### Add icons to a Button

Add icons to Buttons via CSS. Set the "background" property of the Button's `<button/>` element to the url of the icon:

.yui-button#pushbutton2 button, .yui-button#pushbutton5 button, .yui-button#pushbutton8 button { background: url(../button/assets/add.gif) center center no-repeat; text-indent: -4em; overflow: hidden; padding: 0 .75em; width: 2em; *margin-left: 4em; /* IE only */ *padding: 0 1.75em; /* IE only */ } .yui-button#pushbutton3 button, .yui-button#pushbutton6 button, .yui-button#pushbutton9 button { padding-left: 2em; background: url(../button/assets/add.gif) 10% 50% no-repeat; }

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### Button Control (beta) Examples:

- Push Buttons
- Link Buttons
- Checkbox Buttons
- Radio Buttons
- Submit Buttons
- Reset Buttons
- Menu Buttons
- Split Buttons

### More Button Control (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2007 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings