Button Widget


Button Widgetversion added: 1.0

Description: Creates a button widget

QuickNavExamples

Events

Buttons are coded with standard HTML anchor and input elements, then enhanced by jQuery Mobile to make them more attractive and useable on a mobile device. Use anchor links (a elements) to mark up navigation buttons, and input or button elements for form submission.

Styling links as buttons

In the main content block of a page, you can style any anchor link as a button by adding the data-role="button" attribute. The framework will enhance the link with markup and classes to style the link as a button. For example, this markup:

1
<a href="index.html" data-role="button">Link button</a>

Produces this link-based button:

Note: Links styled like buttons have all the same visual options as true form-based buttons below, but there are a few important differences. Link-based buttons aren't part of the button plugin and only just use the underlying buttonMarkup plugin to generate the button styles so the form button methods (enable, disable, refresh) aren't supported. If you need to disable a link-based button (or any element), it's possible to apply the disabled class ui-disabled yourself with JavaScript to achieve the same effect.

1
<a href="index.html" data-role="button" class="ui-disabled">Link button</a>

Produces this disabled link-based button:

Mini version

For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true" attribute to the button to create a mini version.

1
<a href="index.html" data-role="button" data-mini="true">Link button&lt;/a>

This will produce a button that is not as tall as the standard version and has a smaller text size.

Form buttons

For ease of styling, the framework automatically converts any button or input element with a type of submit, reset, or button into a custom styled button - there is no need to add the data-role="button" attribute. However, if needed, you can directly call the button plugin on any selector, just like any jQuery plugin:

1
$( "[type='submit']" ).button();

Button based button:

1
<button>Button element</button>

Input type="button" based button:

1
<input type="button" value="Button">

Input type="submit" based button:

1
<input type="submit" value="Submit Button">

Input type="reset" based button:

1
<input type="reset" value="Reset Button">

Adding Icons to Buttons

The jQuery Mobile framework includes a selected set of icons most often needed for mobile apps. To minimize download size, jQuery Mobile includes a single white icon sprite, and automatically adds a semi-transparent black circle behind the icon to ensure that it has good contrast on any background color.

An icon can be added to a button by adding a data-icon attribute on the anchor specifying the icon to display. For example, the following markup:

1
<a href="index.html" data-role="button" data-icon="delete">Delete</a>

Icon set

The full list of data-icon attributes can found on the icons reference page

.

Icon positioning

By default, all icons in buttons are placed to the left of the button text.

This default may be overridden using the data-iconpos attribute to set the icon to the right, above (top) or below (bottom) the text. For example, the markup:

1
<a href="index.html" data-role="button" data-icon="delete" data-iconpos="right">Delete</a>

You can also create an icon-only button, by setting the data-iconpos attribute to notext. The button plugin will hide the text on-screen, but add it as a title attribute on the link to provide context for screen readers and devices that support tooltips. For example, replacing data-iconpos="right" on the previous example with data-iconpos="notext":

1
<a href="index.html" data-role="button" data-icon="delete" data-iconpos="notext">Delete</a>

Creates this icon-only button:

Mini & Inline

The mini and inline attributes can be added to produce more compact buttons:

Custom Icons

To use custom icons, specify a data-icon value that has a unique name like myapp-email and the button plugin will generate a class by prefixing ui-icon- to the data-icon value and apply it to the button: ui-icon-myapp-email.

You can then write a CSS rule in your stylesheet that targets the ui-icon-myapp-email class to specify the icon background source. To maintain visual consistency with the rest of the icons, create a white icon 18x18 pixels saved as a PNG-8 with alpha transparency.

In this example, we're just pointing to a standalone icon image, but you could just as easily use an icon sprite and specify the positioning instead, just like the icon sprite we use in the framework.

1
2
3
.ui-icon-myapp-email {
background-image: url( "app-icon-email.png" );
}

This will create the standard resolution icon, but many devices now have very high resolution displays, like the retina display on the iPhone 4. To add a HD icon, create an icon that is 36x36 pixels (exactly double the 18 pixel size), and add second a rule that uses the -webkit-min-device-pixel-ratio: 2 media query to target a rule only to high resolution displays. Specify the background image for the HD icon file and set the background size to 18x18 pixels which will fit the 36 pixel icon into the same 18 pixel space. The media query block can wrap multiple icon rules:

1
2
3
4
5
6
7
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
.ui-icon-myapp-email {
background-image: url( "app-icon-email-highres.png" );
background-size: 18px 18px;
}
...more HD icon rules go here...
}

Inline buttons

By default, all buttons in the body content are styled as block-level elements so they fill the width of the screen:

If you have multiple buttons that should sit side-by-side on the same line, add the data-inline="true" attribute to each button. This will style the buttons to be the width of their content and float the buttons so they sit on the same line.

1
2
<a href="index.html" data-role="button" data-inline="true">Cancel</a>
<a href="index.html" data-role="button" data-inline="true" data-theme="b">Save</a>

If you want buttons to sit side-by-side but stretch to fill the width of the screen, you can use the content column grids to put normal full-width buttons into 2- or 3-columns.

Icon example

Grouped buttons

Occasionally, you may want to visually group a set of buttons to form a single block that looks contained like a navigation component. To get this effect, wrap a set of buttons in a container with the data-role="controlgroup" attribute - the framework will create a vertical button group, remove all margins and drop shadows between the buttons, and only round the first and last buttons of the set to create the effect that they are grouped together.

1
2
3
4
5
<div data-role="controlgroup">
<a href="index.html" data-role="button">Yes</a>
<a href="index.html" data-role="button">No</a>
<a href="index.html" data-role="button">Maybe</a>
</div>

By default, grouped buttons are presented as a vertical list:

By adding the data-type="horizontal" attribute to the controlgroup container, you can swap to a horizontal-style group that floats the buttons side-by-side and sets the width to only be large enough to fit the content. (Be aware that these will wrap to multiple lines if the number of buttons or the overall text length is too wide for the screen.)

Labels

Because the label element will be associated with each individual input or button and will be hidden for styling purposes, we recommend wrapping the buttons in a fieldset element that has a legend which acts as the combined label for the group. Using the label as a wrapper around an input prevents the framework from hiding it, so you have to use the for attribute to associate the label with the input.

Theming buttons

jQuery Mobile has a rich theming system that gives you full control of how buttons are styled. When a link is added to a container, it is automatically assigned a theme swatch letter that matches its parent bar or content box to visually integrate the button into the parent container, like a chameleon. So a button placed inside a content container with a theme of "a" (black in the default theme) will be automatically assigned the button theme of "a" (charcoal in the default theme). Here are examples of the button theme pairings in the default theme. All buttons have the same HTML markup:

Assigning theme swatches

Buttons can be manually assigned any of the button color swatches from the theme to add visual contrast with the container they sit inside by adding the data-theme attribute on the button markup and specifying a swatch letter.

1
<a href="index.html" data-role="button" data-theme="a">Swatch a</a>

Here are 5 buttons with icons that have a different swatch letter assigned via the data-theme attribute.

Theme variations

Options

corners 

Type: Boolean
Default: true
Applies the theme button border-radius if set to true.
1
$( "a" ).buttonMarkup({ corners: false });

This option is also exposed as a data attribute: data-corners="false".

Code examples:

Initialize the buttonMarkup with the corners option specified:

1
2
3
$( ".selector" ).buttonMarkup({
corners: false
});

Get or set the corners option, after initialization:

1
2
3
4
5
// Getter
var corners = $( ".selector" ).buttonMarkup( "option", "corners" );
// Setter
$( ".selector" ).buttonMarkup( "option", "corners", false );

icon 

Type: String
Default: null
Applies an icon from the icon set.
1
$( "a" ).buttonMarkup({ icon: "star" });

This option is also exposed as a data attribute: data-icon="star".

Code examples:

Initialize the buttonMarkup with the icon option specified:

1
2
3
$( ".selector" ).buttonMarkup({
icon: "star"
});

Get or set the icon option, after initialization:

1
2
3
4
5
// Getter
var icon = $( ".selector" ).buttonMarkup( "option", "icon" );
// Setter
$( ".selector" ).buttonMarkup( "option", "icon", "star" );

iconpos 

Type: String
Default: "left"
Positions the icon in the button. Possible values: left, right, top, bottom, none, notext. The notext value will display an icon-only button with no text feedback.
1
$( "a" ).buttonMarkup({ iconpos: "right" });

This option is also exposed as a data attribute: data-iconpos="left".

iconshadow 

Type: Boolean
Default: true
Applies the theme shadow to the button's icon if set to true.
1
$( "a" ).buttonMarkup({ iconshadow: false });

This option is also exposed as a data attribute: data-iconshadow="false".

initSelector 

Default: button, [type='button'], [type='submit'], [type='reset']
This is used to define the selectors (element types, data roles, etc.) that will automatically be initialized as form buttons. To change which elements are initialized, bind this option to the mobileinit event:
1
2
3
$( document ).on( "mobileinit", function() {
$.mobile.button.prototype.options.initSelector = ".myButtons";
});

Note: This option applies only to form buttons, which are automatically initialized by the framework.

inline 

Type: Boolean
Default: null (false)
If set to true, this will make the button act like an inline button so the width is determined by the button's text. By default, this is null (false) so the button is full width, regardless of the feedback content. Possible values: true, false.
1
$( "a" ).buttonMarkup({ inline: true });

This option is also exposed as a data attribute: data-inline="true".

mini 

Type: Boolean
Default: null (false)
If set to true, this will display a more compact version of the button that uses less vertical height. Possible values: true, false.
1
$( "a" ).buttonMarkup({ mini: true });

This option is also exposed as a data attribute: data-mini="true".

shadow 

Type: Boolean
Default: true
Applies the drop shadow style to the button if set to true.
1
$( "a" ).buttonMarkup({ shadow: false });

This option is also exposed as a data attribute: data-shadow="false".

theme 

Type: String
Default: null, inherited from parent

Sets the color scheme (swatch) for all instances of this widget. It accepts a single letter from a-z that maps to one of the swatches included in your theme. By default, it will inherit the same swatch color as its parent container if not explicitly set.

1
$( "a" ).buttonMarkup({ theme: "a" });

Possible values: swatch letter (a-z).

This option is also exposed as a data attribute: data-theme="a".

Methods

disable()Returns: jQuery (plugin only)

disable a form button.
1
$( "[type='submit']" ).button( "disable" );
  • This method does not accept any arguments.
Code examples:

Invoke the disable method:

1
$( ".selector" ).buttonMarkup( "disable" );

enable()Returns: jQuery (plugin only)

enable a disabled form button.
1
$( "[type='submit']" ).button( "enable" );
  • This method does not accept any arguments.
Code examples:

Invoke the enable method:

1
$( ".selector" ).buttonMarkup( "enable" );

refresh()Returns: jQuery (plugin only)

update the form button.

If you manipulate a form button via JavaScript, you must call the refresh method on it to update the visual styling.

1
$( "[type='submit']" ).button( "refresh" );
  • This method does not accept any arguments.
Code examples:

Invoke the refresh method:

1
$( ".selector" ).buttonMarkup( "refresh" );

Events

create( event, ui )Type: buttoncreate

triggered when a form button is created

Note: The ui object is empty but included for consistency with other events.

Code examples:

Initialize the buttonMarkup with the create callback specified:

1
2
3
$( ".selector" ).buttonMarkup({
create: function( event, ui ) {}
});

Bind an event listener to the buttoncreate event:

1
$( ".selector" ).on( "buttoncreate", function( event, ui ) {} );

Example:

A basic example of a button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>buttonMarkup demo</title>
<link rel="stylesheet" href="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="//code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>jQuery Mobile Example</h1>
</div>
<div data-role="content">
<a href="index.html" data-role="button">A link button</a>
</div>
</div>
</body>
</html>

Demo: