YUI Library Home

Site Search (YDN & YUIBlog):

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

View example in new window.

Info First Name: Last Name:
Gender: Male Female
Birthday:
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
- Using pre-defined Button Control HTML
- Using no existing HTML

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

<input id="submitbutton1" type="submit" name="submitfield1" value="Submit Form"> <button id="submitbutton2" type="submit" name="submitfield2" value="submitfield2value"> Submit 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 oSubmitButton1 = new YAHOO.widget.Button("submitbutton1", { value: "submitbutton1value" }); // Create a Button using an existing <button> element as a data source var oSubmitButton2 = new YAHOO.widget.Button("submitbutton2");

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

<span id="submitbutton3" class="yui-button yui-submit-button"> <span class="first-child"> <input type="submit" name="submitfield3" value="Submit Form"> </span> </span> <span id="submitbutton4" class="yui-button yui-submit-button"> <span class="first-child"> <button type="button" name="submitfield4">Submit Form</button> </span> </span>

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.

var oSubmitButton3 = new YAHOO.widget.Button("submitbutton3", { value: "submitbutton3value" }); var oSubmitButton4 = new YAHOO.widget.Button("submitbutton4", { type: "submit", value: "submitbutton4value" });

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

var oSubmitButton5 = new YAHOO.widget.Button({ type: "submit", label: "Submit Form", id: "submitbutton5", name: "submitbutton5", value: "submitbutton5value", container: "submitbuttonsfromjavascript" });

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:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Button Control Examples:

### More Button Control Resources:

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

Copyright © 2010 Yahoo! Inc. All rights reserved.

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