This example demonstrates different ways to create a Button that functions like an HTML <a/>
element.
A Link Button can be instantiated three different ways:
<a/>
elementA Link Button can be created using an existing <a/>
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 | <a id="linkbutton1" href="http://www.yahoo.com">Yahoo!</a> |
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 | var oLinkButton1 = new YAHOO.widget.Button("linkbutton1"); |
view plain | print | ? |
A Link Button can also be instantiated using pre-defined Button Control HTML: An element with a class of "yui-button" and "yui-link-button" containing a element with a class of "first-child" containing an <a/>
element:
1 | <span id="linkbutton2" class="yui-button yui-link-button"> |
2 | <span class="first-child"> |
3 | <a href="http://www.yahoo.com">Yahoo!</a> |
4 | </span> |
5 | </span> |
6 | <span id="linkbutton3" class="yui-button yui-link-button"> |
7 | <em class="first-child"> |
8 | <a href="http://www.yahoo.com">Yahoo!</a> |
9 | </em> |
10 | </span> |
view plain | print | ? |
To instantiate a Link Button using the Button Control HTML, pass the id of the Button's root element (the element with the classes "yui-button" and "yui-link-button" applied) as the first argument to constructor and any additional configuration attributes as the second argument via an object literal.
1 | var oLinkButton2 = new YAHOO.widget.Button("linkbutton2"); |
2 | var oLinkButton3 = new YAHOO.widget.Button("linkbutton3"); |
view plain | print | ? |
To build a Link Button without any existing HTML, pass a set of configuration attributes as a single argument to the constructor using an object literal.
1 | var oLinkButton4 = new YAHOO.widget.Button({ |
2 | type: "link", |
3 | id: "linkbutton4", |
4 | label: "Yahoo!", |
5 | href: "http://www.yahoo.com", |
6 | container: "linkbuttonsfromjavascript" }); |
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.
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.
INFO 1294ms (+0) 5:14:14 AM:
Button loggerLink
Initialization completed.
INFO 1294ms (+0) 5:14:14 AM:
Button null
Setting attribute "href" using source element's attribute value of "btn_example02.html"
INFO 1294ms (+9) 5:14:14 AM:
Button null
Building the button using an existing HTML element as a source element.
INFO 1285ms (+638) 5:14:14 AM:
LogReader instance0
LogReader initialized
INFO 647ms (+1) 5:14:13 AM:
Button linkbutton3
Initialization completed.
INFO 646ms (+0) 5:14:13 AM:
Button null
Setting attribute "href" using source element's attribute value of "http://www.yahoo.com"
INFO 646ms (+0) 5:14:13 AM:
Button null
Building the button using an existing HTML element as a source element.
INFO 646ms (+0) 5:14:13 AM:
Button linkbutton2
Initialization completed.
INFO 646ms (+0) 5:14:13 AM:
Button null
Setting attribute "href" using source element's attribute value of "http://www.yahoo.com"
INFO 646ms (+0) 5:14:13 AM:
Button null
Building the button using an existing HTML element as a source element.
INFO 646ms (+1) 5:14:13 AM:
Button linkbutton1
Initialization completed.
INFO 645ms (+0) 5:14:13 AM:
Button null
Source element could not be used as is. Creating a new HTML element for the button.
INFO 645ms (+1) 5:14:13 AM:
Button null
Setting attribute "href" using source element's attribute value of "http://www.yahoo.com"
INFO 644ms (+40) 5:14:13 AM:
Button null
Building the button using an existing HTML element as a source element.
INFO 604ms (+1) 5:14:13 AM:
Button linkbutton6
Initialization completed.
INFO 603ms (+0) 5:14:13 AM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 603ms (+1) 5:14:13 AM:
Button linkbutton5
Initialization completed.
INFO 602ms (+0) 5:14:13 AM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 602ms (+3) 5:14:13 AM:
Button linkbutton4
Initialization completed.
INFO 599ms (+599) 5:14:13 AM:
Button null
No source HTML element. Building the button using the set of configuration attributes.
INFO 0ms (+0) 5:14:13 AM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings