Yahoo! Developer Network Home - Help

Site Search (YDN & YUIBlog):

# YUI Library Examples: Button Control: Reset Buttons

# Button Control: Reset Buttons

This example demonstrates different ways to create a Button that functions like an HTML reset button (`<input type="reset"/>` and `<button type="reset"/>`).

View example in new window.

Info First Name: Last Name:
Gender: Male Female
Birthday:
Reset Buttons
From Markup
From JavaScript

## Creating Reset Buttons

A Reset Button can be instantiated three different ways:

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

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

A Reset Button can be created using an existing `<input type="reset"/>` or `<button type="reset"/>` 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.

<input id="resetbutton1" type="reset" name="resetfield1" value="Reset Form"> <button id="resetbutton2" type="reset" name="resetfield2">Reset Form</button>

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.

// Create a Button using an existing <input> element as a data source var oResetButton1 = new YAHOO.widget.Button("resetbutton1"); // Create a Button using an existing <button> element as a data source var oResetButton2 = new YAHOO.widget.Button("resetbutton2");

The Button's constructor will look for the existing `<input type="reset"/>` or `<button type="reset"/>` element and, once it is found, it will be swapped out for the newly created element via the DOM's `replaceChild` method.

### Using pre-defined Button Control HTML

A Reset Button can also be instantiated using pre-defined Button Control HTML: An element with a class of "yui-button" and "yui-reset-button" containing a element with a class of "first-child" containing either a `<input type="reset"/>` or `<button type="button"/>` element:

<span id="resetbutton3" class="yui-button yui-reset-button"> <span class="first-child"> <input type="reset" name="resetfield3" value="Reset Form"> </span> </span> <span id="resetbutton4" class="yui-button yui-reset-button"> <span class="first-child"> <button type="button" name="resetfield4">Reset Form</button> </span> </span>

To instantiate a Reset Button using the Button Control HTML, pass the id of the Button's root element (the element with the classes "yui-button" and "yui-reset-button" applied) as the first argument to constructor and any additional configuration attributes as the second argument via an object literal.

var oResetButton3 = new YAHOO.widget.Button("resetbutton3"); var oResetButton4 = new YAHOO.widget.Button("resetbutton4", { type: "reset" });

### Using no existing HTML

To build a Reset Button without any existing HTML, pass a set of configuration attributes as a single argument to the constructor using an object literal.

var oResetButton5 = new YAHOO.widget.Button({ type: "reset", label: "Reset Form", id: "resetfield5", container: "resetbuttonsfromjavascript" });

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.

### 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 Examples:

### More Button Control Resources:

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

Copyright © 2008 Yahoo! Inc. All rights reserved.

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