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.

YUI Logger Output:

Logger Console

INFO1093ms (+1) 3:04:41 PM:

Button loggerLink

Initialization completed.

INFO1092ms (+0) 3:04:41 PM:

Button loggerLink

Setting attribute "href" using source element's attribute value of "btn_example06.html"

INFO1092ms (+1) 3:04:41 PM:

Button loggerLink

Building the button using an existing HTML element as a source element.

INFO1091ms (+0) 3:04:41 PM:

Button resetbutton4

Initialization completed.

INFO1091ms (+0) 3:04:41 PM:

Button resetbutton4

Setting attribute "name" using source element's attribute value of "resetfield4"

INFO1091ms (+0) 3:04:41 PM:

Button resetbutton4

Building the button using an existing HTML element as a source element.

INFO1091ms (+1) 3:04:41 PM:

Button resetbutton3

Initialization completed.

INFO1090ms (+0) 3:04:41 PM:

Button resetbutton3

Setting attribute "value" using source element's attribute value of "Reset Form"

INFO1090ms (+0) 3:04:41 PM:

Button resetbutton3

Setting attribute "name" using source element's attribute value of "resetfield3"

INFO1090ms (+0) 3:04:41 PM:

Button resetbutton3

Setting attribute "type" using source element's attribute value of "reset"

INFO1090ms (+0) 3:04:41 PM:

Button resetbutton3

Building the button using an existing HTML element as a source element.

INFO1090ms (+1) 3:04:41 PM:

Button resetbutton2

Initialization completed.

INFO1089ms (+0) 3:04:41 PM:

Button resetbutton2

Source element could not be used as is. Creating a new HTML element for the button.

INFO1089ms (+0) 3:04:41 PM:

Button resetbutton2

Setting attribute "name" using source element's attribute value of "resetfield2"

INFO1089ms (+1) 3:04:41 PM:

Button resetbutton2

Setting attribute "type" using source element's attribute value of "reset"

INFO1088ms (+0) 3:04:41 PM:

Button resetbutton2

Building the button using an existing HTML element as a source element.

INFO1088ms (+9) 3:04:41 PM:

Button resetbutton1

Initialization completed.

INFO1079ms (+0) 3:04:41 PM:

Button resetbutton1

Source element could not be used as is. Creating a new HTML element for the button.

INFO1079ms (+0) 3:04:41 PM:

Button resetbutton1

Setting attribute "value" using source element's attribute value of "Reset Form"

INFO1079ms (+0) 3:04:41 PM:

Button resetbutton1

Setting attribute "name" using source element's attribute value of "resetfield1"

INFO1079ms (+1) 3:04:41 PM:

Button resetbutton1

Setting attribute "type" using source element's attribute value of "reset"

INFO1078ms (+17) 3:04:41 PM:

Button resetbutton1

Building the button using an existing HTML element as a source element.

INFO1061ms (+145) 3:04:41 PM:

LogReader instance0

LogReader initialized

INFO916ms (+3) 3:04:41 PM:

Button resetfield5

Initialization completed.

INFO913ms (+913) 3:04:41 PM:

Button resetfield5

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

INFO0ms (+0) 3:04:40 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 © 2008 Yahoo! Inc. All rights reserved.

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