YUI Library Home

YUI Library Examples: Button Control: Submit Buttons

Button Control: Submit Buttons

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

Info
Submit Buttons
From Markup
From JavaScript

Creating Submit Buttons

A Submit Button can be instantiated three different ways:

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

A Submit Button can be created using an existing <input type="submit"/> or <button type="submit"/> 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="submitbutton1" type="submit" name="submitfield1" value="Submit Form"
2<button id="submitbutton2" type="submit" name="submitfield2" value="submitfield2value"
3    Submit Form 
4</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 oSubmitButton1 = new YAHOO.widget.Button("submitbutton1", { value: "submitbutton1value" }); 
4 
5// Create a Button using an existing <button> element as a data source 
6 
7var oSubmitButton2 = new YAHOO.widget.Button("submitbutton2"); 
view plain | print | ?

Using pre-defined Button Control HTML

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

1<span id="submitbutton3" class="yui-button yui-submit-button"
2    <span class="first-child"
3        <input type="submit" name="submitfield3" value="Submit Form"
4    </span> 
5</span> 
6<span id="submitbutton4" class="yui-button yui-submit-button"
7    <span class="first-child"
8        <button type="button" name="submitfield4">Submit Form</button> 
9    </span> 
10</span> 
view plain | print | ?

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

1var oSubmitButton3 = new YAHOO.widget.Button("submitbutton3", { value: "submitbutton3value" }); 
2 
3var oSubmitButton4 = new YAHOO.widget.Button("submitbutton4", { type: "submit", value:  "submitbutton4value" });         
view plain | print | ?

Using no existing HTML

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

1var oSubmitButton5 = new YAHOO.widget.Button({  
2                                type: "submit",  
3                                label: "Submit Form",  
4                                id: "submitbutton5",  
5                                name: "submitbutton5",  
6                                value: "submitbutton5value",  
7                                container: "submitbuttonsfromjavascript" }); 
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 132ms (+1) 1:08:53 PM:

Button loggerLink

Initialization completed.

INFO 131ms (+0) 1:08:53 PM:

Button null

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

INFO 131ms (+6) 1:08:53 PM:

Button null

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

INFO 125ms (+0) 1:08:53 PM:

Button submitbutton4

Initialization completed.

INFO 125ms (+0) 1:08:53 PM:

Button null

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

INFO 125ms (+1) 1:08:53 PM:

Button null

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

INFO 124ms (+0) 1:08:53 PM:

Button submitbutton3

Initialization completed.

INFO 124ms (+0) 1:08:53 PM:

Button null

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

INFO 124ms (+0) 1:08:53 PM:

Button null

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

INFO 124ms (+0) 1:08:53 PM:

Button null

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

INFO 124ms (+1) 1:08:53 PM:

Button submitbutton2

Initialization completed.

INFO 123ms (+0) 1:08:53 PM:

Button null

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

INFO 123ms (+0) 1:08:53 PM:

Button null

Setting attribute "value" using source element's attribute value of "submitfield2value"

INFO 123ms (+0) 1:08:53 PM:

Button null

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

INFO 123ms (+0) 1:08:53 PM:

Button null

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

INFO 123ms (+0) 1:08:53 PM:

Button null

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

INFO 123ms (+2) 1:08:53 PM:

Button submitbutton1

Initialization completed.

INFO 121ms (+0) 1:08:53 PM:

Button null

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

INFO 121ms (+0) 1:08:53 PM:

Button null

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

INFO 121ms (+0) 1:08:53 PM:

Button null

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

INFO 121ms (+1) 1:08:53 PM:

Button null

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

INFO 120ms (+67) 1:08:53 PM:

LogReader instance0

LogReader initialized

INFO 53ms (+3) 1:08:53 PM:

Button submitbutton5

Initialization completed.

INFO 50ms (+50) 1:08:53 PM:

Button null

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

INFO 0ms (+0) 1:08:52 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 © 2009 Yahoo! Inc. All rights reserved.

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