YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

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"/>).

Info
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

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.

1<input id="resetbutton1" type="reset" name="resetfield1" value="Reset Form"
2<button id="resetbutton2" type="reset" name="resetfield2">Reset Form</button> 
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// Create a Button using an existing <input> element as a data source 
2 
3var oResetButton1 = new YAHOO.widget.Button("resetbutton1"); 
4 
5// Create a Button using an existing <button> element as a data source 
6 
7var oResetButton2 = new YAHOO.widget.Button("resetbutton2"); 
view plain | print | ?

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:

1<span id="resetbutton3" class="yui-button yui-reset-button"
2    <span class="first-child"
3        <input type="reset" name="resetfield3" value="Reset Form"
4    </span> 
5</span> 
6<span id="resetbutton4" class="yui-button yui-reset-button"
7    <span class="first-child"
8        <button type="button" name="resetfield4">Reset Form</button> 
9    </span> 
10</span> 
view plain | print | ?

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.

1var oResetButton3 = new YAHOO.widget.Button("resetbutton3"); 
2 
3var oResetButton4 = new YAHOO.widget.Button("resetbutton4", { type: "reset" }); 
view plain | print | ?

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.

1var oResetButton5 = new YAHOO.widget.Button({ type: "reset",  
2                                label: "Reset Form",  
3                                id: "resetfield5",  
4                                container:  "resetbuttonsfromjavascript" }); 
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.

Configuration for This Example

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.

YUI Logger Output:

Logger Console

INFO 47ms (+1) 9:55:26 PM:

LogReader instance0

LogReader initialized

INFO 46ms (+0) 9:55:26 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 46ms (+0) 9:55:26 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 46ms (+40) 9:55:26 PM:

Get

_next: q0, loaded: undefined

INFO 6ms (+3) 9:55:26 PM:

Button resetfield5

Initialization completed.

INFO 3ms (+3) 9:55:26 PM:

Button null

No source HTML element. Building the button using the set of configuration attributes.

INFO 0ms (+0) 9:55:26 PM:

global

Logger initialized

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

Reload with logging
and debugging disabled.

More Button Control Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

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