Yahoo! Developer Network Home - Help

YUI Library Examples: Button Control: Checkbox Buttons

Button Control: Checkbox Buttons

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

Checkbox Buttons
From Markup
From JavaScript

Creating Checkbox Buttons

A Checkbox Button can be instantiated three different ways:

Using an existing <input type="checkbox"/> element

A Checkbox Button can be created using an existing <input type="checkbox"/> 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="checkbutton1" type="checkbox" name="checkboxfield1" value="1" checked> 
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.

1var oCheckButton1 = new YAHOO.widget.Button("checkbutton1", { label:"One" }); 
view plain | print | ?

Using pre-defined Button Control HTML

A Checkbox Button can also be instantiated using pre-defined Button Control HTML: An element with a class of "yui-button" and "yui-checkbox-button" containing a element with a class of "first-child" containing a <button/> element:

1<span id="checkbutton5" class="yui-button yui-checkbox-button"
2    <span class="first-child"
3        <button type="button" name="checkboxfield2">One</button> 
4    </span> 
5</span> 
view plain | print | ?

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

1var oCheckButton5 = new YAHOO.widget.Button("checkbutton5", {  
2                                        type:"checkbox",  
3                                        value:"1",  
4                                        checked:true }); 
view plain | print | ?

Using no existing HTML

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

1var oCheckButton9 = new YAHOO.widget.Button({  
2                            type: "checkbox",  
3                            label: "One",  
4                            id: "checkbutton9",  
5                            name: "checkboxfield3",  
6                            value: "1",  
7                            container: "checkboxbuttonsfromjavascript",  
8                            checked: true }); 
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

INFO1585ms (+1) 9:22:24 PM:

Button loggerLink

Initialization completed.

INFO1584ms (+0) 9:22:24 PM:

Button loggerLink

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

INFO1584ms (+11) 9:22:24 PM:

Button loggerLink

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

INFO1573ms (+319) 9:22:24 PM:

LogReader instance0

LogReader initialized

INFO1254ms (+1) 9:22:24 PM:

Button checkbutton8

Initialization completed.

INFO1253ms (+0) 9:22:24 PM:

Button checkbutton8

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

INFO1253ms (+0) 9:22:24 PM:

Button checkbutton8

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

INFO1253ms (+0) 9:22:24 PM:

Button checkbutton7

Initialization completed.

INFO1253ms (+0) 9:22:24 PM:

Button checkbutton7

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

INFO1253ms (+0) 9:22:24 PM:

Button checkbutton7

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

INFO1253ms (+1) 9:22:24 PM:

Button checkbutton6

Initialization completed.

INFO1252ms (+0) 9:22:24 PM:

Button checkbutton6

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

INFO1252ms (+0) 9:22:24 PM:

Button checkbutton6

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

INFO1252ms (+0) 9:22:24 PM:

Button checkbutton5

Initialization completed.

INFO1252ms (+1) 9:22:24 PM:

Button checkbutton5

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton5

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

Initialization completed.

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

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

INFO1251ms (+0) 9:22:24 PM:

Button checkbutton4

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

INFO1251ms (+1) 9:22:24 PM:

Button checkbutton3

Initialization completed.

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton3

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton3

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton3

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton3

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton3

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

Initialization completed.

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

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

INFO1250ms (+0) 9:22:24 PM:

Button checkbutton2

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

INFO1250ms (+2) 9:22:24 PM:

Button checkbutton1

Initialization completed.

INFO1248ms (+0) 9:22:24 PM:

Button checkbutton1

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

INFO1248ms (+0) 9:22:24 PM:

Button checkbutton1

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

INFO1248ms (+0) 9:22:24 PM:

Button checkbutton1

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

INFO1248ms (+0) 9:22:24 PM:

Button checkbutton1

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

INFO1248ms (+29) 9:22:24 PM:

Button checkbutton1

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

INFO1219ms (+0) 9:22:24 PM:

Button checkbutton12

Initialization completed.

INFO1219ms (+0) 9:22:24 PM:

Button checkbutton12

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

INFO1219ms (+0) 9:22:24 PM:

Button checkbutton11

Initialization completed.

INFO1219ms (+0) 9:22:24 PM:

Button checkbutton11

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

INFO1219ms (+1) 9:22:24 PM:

Button checkbutton10

Initialization completed.

INFO1218ms (+0) 9:22:24 PM:

Button checkbutton10

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

INFO1218ms (+3) 9:22:24 PM:

Button checkbutton9

Initialization completed.

INFO1215ms (+1215) 9:22:24 PM:

Button checkbutton9

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

INFO0ms (+0) 9:22:23 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