YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Button Control: Link Buttons

# Button Control: Link Buttons

This example demonstrates different ways to create a Button that functions like an HTML `<a/>` element.

View example in new window.

## From Markup

Yahoo! Yahoo! Yahoo!

## From JavaScript

## Creating Link Buttons

A Link Button can be instantiated three different ways:

- Using an existing `<a/>` element
- Using pre-defined Button Control HTML
- Using no existing HTML

### Using an existing `<a/>` element

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

<a id="linkbutton1" href="http://www.yahoo.com">Yahoo!</a>

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.

var oLinkButton1 = new YAHOO.widget.Button("linkbutton1");

### Using pre-defined Button Control HTML

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:

<span id="linkbutton2" class="yui-button yui-link-button"> <span class="first-child"> <a href="http://www.yahoo.com">Yahoo!</a> </span> </span> <span id="linkbutton3" class="yui-button yui-link-button"> <em class="first-child"> <a href="http://www.yahoo.com">Yahoo!</a> </em> </span>

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.

var oLinkButton2 = new YAHOO.widget.Button("linkbutton2"); var oLinkButton3 = new YAHOO.widget.Button("linkbutton3");

### Using no existing HTML

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.

var oLinkButton4 = new YAHOO.widget.Button({ type: "link", id: "linkbutton4", label: "Yahoo!", href: "http://www.yahoo.com", container: "linkbuttonsfromjavascript" });

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: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

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