Selectmenu Widget


Selectmenu Widgetversion added: 1.0

Description: Creates a select menu widget

QuickNavExamples

Events

The select menu is based on a native select element, which is hidden from view and replaced with a custom-styled select button that matches the look and feel of the jQuery Mobile framework. The select menu is ARIA-enabled and keyboard accessible on the desktop as well.

By default, the framework leverages the native OS options menu to use with the custom button. When the button is clicked, the native OS menu will open. When a value is selected and the menu closes, the custom button's text is updated to match the selected value. Please note that the framework also offers the possibility of having custom (non-native) select menus.

To add a select menu to your page, start with a standard select element populated with a set of option elements. Set the for attribute of the label to match the id of the select so they are semantically associated. It's possible to accessibly hide the label if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.

The framework will find all select elements and automatically enhance them into select menus, no need to apply a data-role attribute. To prevent the automatic enhancement of a select, add data-role="none" attribute to the select.

1
2
3
4
5
6
7
<label for="select-choice-0" class="select">Shipping method:</label>
<select name="select-choice-0" id="select-choice-0">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>

This will produce a basic select menu. The default styles set the width of the input to 100% of the parent container and stacks the label on a separate line.

Mini version

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

1
2
3
4
5
6
7
<label for="select-choice-min" class="select">Shipping method:</label>
<select name="select-choice-min" id="select-choice-min" data-mini="true">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>

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

Field containers

Optionally wrap the selects in a container with class ui-field-contain to help visually group it in a longer form.

Note: The data- attribute data-role="fieldcontain" is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0. Add class ui-field-contain instead.

1
2
3
4
5
6
7
8
9
<div class="ui-field-contain">
<label for="select-choice-1" class="select">Shipping method:</label>
<select name="select-choice-1" id="select-choice-1">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>
</div>

The select input is now displayed like this:

An example of a select with a long list of options:

Optgroups

The following example organizes the options into optgroup elements. Support for this feature in mobile selects is a bit spotty, but is improving.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<div class="ui-field-contain">
<label for="select-choice-nc" class="select">Preferred delivery:</label>
<select name="select-choice-8" id="select-choice-nc">
<optgroup label="FedEx">
<option value="firstOvernight">First Overnight</option>
<option value="expressSaver">Express Saver</option>
<option value="ground">Ground</option>
</optgroup>
<optgroup label="UPS">
<option value="firstOvernight">First Overnight</option>
<option value="expressSaver">Express Saver</option>
<option value="ground">Ground</option>
</optgroup>
<optgroup label="US Mail">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day (disabled)</option>
<option value="overnight">Overnight</option>
</optgroup>
</select>
</div>

Vertically grouped select inputs

To create a grouped set of select inputs, first add select and a corresponding label. Set the for attribute of the label to match the id of the select so they are semantically associated.

Because the label element will be associated with each individual select input and will be hidden for styling purposes, we recommend wrapping the selects in a fieldset element that has a legend which acts as the combined label for the grouped inputs.

Lastly, one needs to wrap the fieldset in a div with data-role="controlgroup" attribute, so it can be styled as a group.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<div class="ui-field-contain">
<fieldset data-role="controlgroup">
<legend>Date of Birth:</legend>
<label for="select-choice-month">Month</label>
<select name="select-choice-month" id="select-choice-month">
<option>Month</option>
<option value="jan">January</option>
<!-- etc. -->
</select>
<label for="select-choice-day">Day</label>
<select name="select-choice-day" id="select-choice-day">
<option>Day</option>
<option value="1">1</option>
<!-- etc. -->
</select>
<label for="select-choice-year">Year</label>
<select name="select-choice-year" id="select-choice-year">
<option>Year</option>
<option value="2011">2011</option>
<!-- etc. -->
</select>
</fieldset>
</div>

Horizontally grouped select inputs

Select inputs can also be used for grouped sets with more than one related selections. To make a horizontal button set, add the data-type="horizontal" to the fieldset. Note that the buttons which trigger the select will resize depending on the currently selected option’s value.

1
<fieldset data-role="controlgroup" data-type="horizontal">

Theming selects

You can specify any jQuery Mobile button data- attribute on a select element, too. In this example, we're setting the theme, icon and inline properties:

Custom select menus

The framework is capable of building a custom menu based on the select element's list of options. We recommend using a custom menu when multiple selections are required, or when the menu itself must be styled with CSS.

You can optionally use custom-styled select menus instead of the native OS menu. The custom menu supports disabled options and multiple selection (whereas native mobile OS support for both is inconsistent), adds an elegant way to handle placeholder values, and restores missing functionality on certain platforms such as optgroup support on Android (all explained below). In addition, the framework applies the custom button's theme to the menu to better match the look and feel and provide visual consistency across platforms. Lastly, custom menus often look better on desktop browsers because native desktop menus are smaller than their mobile counterparts and tend to look disproportionate.

Keep in mind that there is overhead involved in parsing the native select to build a custom menu. If there are a lot of selects on a page, or a select has a long list of options, this can impact the performance of the page, so we recommend using custom menus sparingly.

To use custom menus on a specific select, just add the data-native-menu="false" attribute. Alternately, this can also programmatically set the select menu's nativeMenu configuration option to false in a callback bound to the mobileinit event to achieve the same effect. This will globally make all selects use the custom menu by default. The following must be included in the page after jQuery is loaded but before jQuery Mobile is loaded.

1
2
3
$(document).on( "mobileinit", function() {
$.mobile.selectmenu.prototype.options.nativeMenu = false;
});

When the select has a small number of options that will fit on the device's screen, the menu will appear as a small overlay with a pop transition:

When it has too many options to show on the device's screen, the framework will automatically create a new dialog-style "page" populated with a standard listview for the options. This allows us to use the native scrolling included on the device for moving through a long list. The text inside the label is used as the title for this page. Be aware of the page and pagecontainer events that will be fired for this generated page.

Note: The behavior whereby the custom select menu creates a new page when the list of options is long is deprecated as of jQuery Mobile 1.4.0. Starting with 1.5.0, the custom select menu will fall back to utilizing the select menu's native behavior when the list of options is too long.

Disabled options

jQuery Mobile will automatically disable and style option tags with the disabled attribute. In the demo below, the second option "Rush: 3 days" has been set to disabled.

Placeholder options

It's common for developers to include a "null" option in their select element to force a user to choose an option. If a placeholder option is present in your markup, jQuery Mobile will hide them in the overlay menu, showing only valid choices to the user, and display the placeholder text inside the menu as a header. A placeholder option is added when the framework finds:

  • An option with no value attribute
  • An option with an empty value attribute (value="").

    Note: Indicating that an option should be used as a placeholder by providing the value attribute and setting it to "" is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0.

  • An option with no text node
  • An option with a data-placeholder="true" attribute. (This allows you to use an option that has a value and a textnode as a placeholder option).

You can disable this feature through the selectmenu plugin's hidePlaceholderMenuItems option, like this:

1
$.mobile.selectmenu.prototype.options.hidePlaceholderMenuItems = false;

Examples of various placeholder options:

Multiple selects

If the multiple attribute is present in your markup, jQuery Mobile will enhance the element with a few extra considerations:

  • A header element will be created inside the menu and display the placeholder text and a close button.
  • Clicking on an item inside the overlay menu will not close the widget.
  • A ghosted, unchecked icon will appear adjacent to each unselected item. When the item is selected the icon will change to a checkbox. Neither icon will appear inside a single select box.
  • Once 2+ items are selected, a counter element with the total number of selected items will appear inside the button.
  • The text of each selected item will appear inside the button as a list. If the button is not wide enough to display the entire list, it is truncated with an ellipses.
  • If no items are selected, the button's text will default to the placeholder text.
  • If no placeholder element exists, the default button text will be blank and the header will appear with just a close button. Because this isn't a friendly user experience, we recommended that you always specify a placeholder element when using multiple select boxes.
  • Currently jQuery Mobile only supports the multiple attribute on a select with nativeMenu set to false.

When a select is large enough to where the menu will open in a new page, the placeholder text is displayed in the button when no items are selected, and the label text is displayed in the menu's header. This differs from smaller overlay menus where the placeholder text is displayed in both the button and the header, and from full-page single selects where the placeholder text is not used at all.

Optgroup support

If a select menu contains optgroup elements, jQuery Mobile will create a divider & group items based on the label attribute's text:

Theming selects

You can specify any jQuery Mobile button data- attribute on a select element, too. In this example, we're setting the theme, icon and inline properties:

The data-overlay-theme attribute can be added to a select element to set the color of the overlay layer for the dialog-based custom select menus and the outer border of the smaller custom menus. By default, the content block colors for swatch "a"" will be used for the overlays.

Calling the select menu plugin

The select menu plugin will auto initialize on any page that contains a select menu, without any need for a data-role attribute in the markup. However, you can directly call the select menu plugin on any selector, just like any normal jQuery plugin:

1
$( "select" ).selectmenu();

Options

closeTextType:

Default: "Close"

Customizes the text of the close button which is helpful for translating this into different languages. The close button is displayed as an icon-only button by default so the text isn't visible on-screen, but is read by screen readers so this is an important accessibility feature.

This option is also exposed as a data attribute: data-close-text="Fermer".

Code examples:

Initialize the selectmenu with the closeText option specified:

1
$( ".selector" ).selectmenu({ closeText: "Fermer" });

Get or set the closeText option, after initialization:

1
2
3
4
5
// getter
var closeText = $( ".selector" ).selectmenu( "option", "closeText" );
// setter
$( ".selector" ).selectmenu( "option", "closeText", "Fermer" );

cornersType: Boolean

Default: true
Applies the theme button border-radius to the select button if set to true.

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

Code examples:

Initialize the selectmenu with the corners option specified:

1
$( ".selector" ).selectmenu({ corners: false });

Get or set the corners option, after initialization:

1
2
3
4
5
// getter
var corners = $( ".selector" ).selectmenu( "option", "corners" );
// setter
$( ".selector" ).selectmenu( "option", "corners", false );

defaultsType: Boolean

Default: false
Seting this option to true indicates that other widgets options have default values and causes jQuery Mobile's widget autoenhancement code to omit the step where it retrieves option values from data attributes. This can improve startup time.

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

Code examples:

Initialize the selectmenu with the defaults option specified:

1
$( ".selector" ).selectmenu({ defaults: true });

Get or set the defaults option, after initialization:

1
2
3
4
5
// getter
var defaults = $( ".selector" ).selectmenu( "option", "defaults" );
// setter
$( ".selector" ).selectmenu( "option", "defaults", true );

disabledType: Boolean

Default: false
Disables the selectmenu if set to true.

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

Code examples:

Initialize the selectmenu with the disabled option specified:

1
$( ".selector" ).selectmenu({ disabled: true });

Get or set the disabled option, after initialization:

1
2
3
4
5
// getter
var disabled = $( ".selector" ).selectmenu( "option", "disabled" );
// setter
$( ".selector" ).selectmenu( "option", "disabled", true );

dividerThemeType: String

Default: null, inherited from parent
Sets the color scheme (swatch) for the listview dividers that represent the optgroup headers. It accepts a single letter from a-z that maps to the swatches included in your theme.

Possible values: swatch letter (a-z).

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

Code examples:

Initialize the selectmenu with the dividerTheme option specified:

1
$( ".selector" ).selectmenu({ dividerTheme: "b" });

Get or set the dividerTheme option, after initialization:

1
2
3
4
5
// getter
var dividerTheme = $( ".selector" ).selectmenu( "option", "dividerTheme" );
// setter
$( ".selector" ).selectmenu( "option", "dividerTheme", "b" );

hidePlaceholderMenuItemsType: Boolean

Default: true
Sets whether placeholder menu items are hidden. When true, the menu item used as the placeholder for the select menu widget will not appear in the list of choices.

This option is also exposed as a data attribute: data-hide-placeholder-menu-items="false".

Code examples:

Initialize the selectmenu with the hidePlaceholderMenuItems option specified:

1
$( ".selector" ).selectmenu({ hidePlaceholderMenuItems: false });

Get or set the hidePlaceholderMenuItems option, after initialization:

1
2
3
4
5
// getter
var hidePlaceholderMenuItems = $( ".selector" ).selectmenu( "option", "hidePlaceholderMenuItems" );
// setter
$( ".selector" ).selectmenu( "option", "hidePlaceholderMenuItems", false );

iconType: String

Default: "carat-d"
Replaces the default icon "carat-d" with an icon from the icon set. Setting this attribute to "false" suppresses the icon.

To suppress the icon, a boolean expression must be used:

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

Code examples:

Initialize the selectmenu with the icon option specified:

1
$( ".selector" ).selectmenu({ icon: "star" });

Get or set the icon option, after initialization:

1
2
3
4
5
// getter
var icon = $( ".selector" ).selectmenu( "option", "icon" );
// setter
$( ".selector" ).selectmenu( "option", "icon", "star" );

iconposType: String

Default: "right"
Position of the icon in the select button. Possible values: left, right, top, bottom, notext. The notext value will display the select as an icon-only button with no text feedback.

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

Code examples:

Initialize the selectmenu with the iconpos option specified:

1
$( ".selector" ).selectmenu({ iconpos: "left" });

Get or set the iconpos option, after initialization:

1
2
3
4
5
// getter
var iconpos = $( ".selector" ).selectmenu( "option", "iconpos" );
// setter
$( ".selector" ).selectmenu( "option", "iconpos", "left" );

iconshadowType: Boolean

Default: true
This option is deprecated in 1.4.0 and will be removed in 1.5.0.

Applies the theme shadow to the select button's icon if set to true.

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

(version deprecated: 1.4.0)
Code examples:

Initialize the selectmenu with the iconshadow option specified:

1
$( ".selector" ).selectmenu({ iconshadow: false });

Get or set the iconshadow option, after initialization:

1
2
3
4
5
// getter
var iconshadow = $( ".selector" ).selectmenu( "option", "iconshadow" );
// setter
$( ".selector" ).selectmenu( "option", "iconshadow", false );

initSelectorType: Selector

Default: See below

The default initSelector for the selectmenu widget is:

1
"select:not( :jqmData(role='slider')):not( :jqmData(role='flipswitch') )"

Note: This option is deprecated in 1.4.0 and will be removed in 1.5.0.
As of jQuery Mobile 1.4.0, the initSelector is no longer a widget option. Instead, it is declared directly on the widget prototype. Thus, you may specify a custom value by handling the mobileinit event and overwriting the initSelector on the prototype:

1
2
3
$( document ).on( "mobileinit", function() {
$.mobile.selectmenu.prototype.initSelector = "div.custom";
});

Note: Remember to attach the mobileinit handler after you have loaded jQuery, but before you load jQuery Mobile, because the event is triggered as part of jQuery Mobile's loading process.

The value of this option is a jQuery selector string. The framework selects elements based on the value of this option and instantiates selectmenu widgets on each of the resulting list of elements.

(version deprecated: 1.4.0)

inlineType: Boolean

Default: null (false)
If set to true, this will make the select button act like an inline button so the width is determined by the button's text. By default, this is null (false) so the select button is full width, regardless of the feedback content. Possible values: true, false.

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

Code examples:

Initialize the selectmenu with the inline option specified:

1
$( ".selector" ).selectmenu({ inline: true });

Get or set the inline option, after initialization:

1
2
3
4
5
// getter
var inline = $( ".selector" ).selectmenu( "option", "inline" );
// setter
$( ".selector" ).selectmenu( "option", "inline", true );

miniType: Boolean

Default: null (false)
If set to true, this will display a more compact version of the selectmenu that uses less vertical height by applying the ui-mini class to the outermost element of the selectmenu widget.

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

nativeMenuType: Boolean

Default: true
When set to true, clicking the custom-styled select menu will open the native select menu which is best for performance. If set to false, the custom select menu style will be used instead of the native menu.

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

Code examples:

Initialize the selectmenu with the nativeMenu option specified:

1
$( ".selector" ).selectmenu({ nativeMenu: false });

Get or set the nativeMenu option, after initialization:

1
2
3
4
5
// getter
var nativeMenu = $( ".selector" ).selectmenu( "option", "nativeMenu" );
// setter
$( ".selector" ).selectmenu( "option", "nativeMenu", false );

overlayThemeType: String

Default: "null, inherited from parent"
Sets the color of the overlay layer for the dialog-based custom select menus and the outer border of the smaller custom menus. It accepts a single letter from a-z that maps to the swatches included in your theme. By default, the content block colors for the overlay will be inherited from the parent of the select.

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

Code examples:

Initialize the selectmenu with the overlayTheme option specified:

1
$( ".selector" ).selectmenu({ overlayTheme: "a" });

Get or set the overlayTheme option, after initialization:

1
2
3
4
5
// getter
var overlayTheme = $( ".selector" ).selectmenu( "option", "overlayTheme" );
// setter
$( ".selector" ).selectmenu( "option", "overlayTheme", "a" );

preventFocusZoomType: Boolean

Default: true on iOS platforms
This option disables page zoom temporarily when a custom select is focused, which prevents iOS devices from zooming the page into the select. By default, iOS often zooms into form controls, and the behavior is often unnecessary and intrusive in mobile-optimized layouts.

This option is also exposed as a data attribute: data-prevent-focus-zoom="true".

Code examples:

Initialize the selectmenu with the preventFocusZoom option specified:

1
$( ".selector" ).selectmenu({ preventFocusZoom: false });

Get or set the preventFocusZoom option, after initialization:

1
2
3
4
5
// getter
var preventFocusZoom = $( ".selector" ).selectmenu( "option", "preventFocusZoom" );
// setter
$( ".selector" ).selectmenu( "option", "preventFocusZoom", false );

shadowType: Boolean

Default: true
Applies the drop shadow style to the select button if set to true.

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

Code examples:

Initialize the selectmenu with the shadow option specified:

1
$( ".selector" ).selectmenu({ shadow: false });

Get or set the shadow option, after initialization:

1
2
3
4
5
// getter
var shadow = $( ".selector" ).selectmenu( "option", "shadow" );
// setter
$( ".selector" ).selectmenu( "option", "shadow", false );

themeType: String

Default: null, inherited from parent
Sets the color scheme (swatch) for the selectmenu widget. It accepts a single letter from a-z that maps to the swatches included in your theme.

Possible values: swatch letter (a-z).

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

Code examples:

Initialize the selectmenu with the theme option specified:

1
$( ".selector" ).selectmenu({ theme: "b" });

Get or set the theme option, after initialization:

1
2
3
4
5
// getter
var theme = $( ".selector" ).selectmenu( "option", "theme" );
// setter
$( ".selector" ).selectmenu( "option", "theme", "b" );

Methods

close()Returns: jQuery (plugin only)

close an open select menu.
  • This method does not accept any arguments.
Code examples:

Invoke the close method:

1
$( ".selector" ).selectmenu( "close" );

destroy()Returns: jQuery (plugin only)

Removes the selectmenu functionality completely. This will return the element back to its pre-init state.
  • This method does not accept any arguments.
Code examples:

Invoke the destroy method:

1
$( ".selector" ).selectmenu( "destroy" );

disable()Returns: jQuery (plugin only)

Disables the selectmenu.
  • This method does not accept any arguments.
Code examples:

Invoke the disable method:

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

enable()Returns: jQuery (plugin only)

Enables the selectmenu.
  • This method does not accept any arguments.
Code examples:

Invoke the enable method:

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

open()Returns: jQuery (plugin only)

open a closed select menu (custom menus only).
  • This method does not accept any arguments.
Code examples:

Invoke the open method:

1
$( ".selector" ).selectmenu( "open" );

option( optionName )Returns: Object

Gets the value currently associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to get.
Code examples:

Invoke the method:

1
var isDisabled = $( ".selector" ).selectmenu( "option", "disabled" );

option()Returns: PlainObject

Gets an object containing key/value pairs representing the current selectmenu options hash.
  • This signature does not accept any arguments.
Code examples:

Invoke the method:

1
var options = $( ".selector" ).selectmenu( "option" );

option( optionName, value )Returns: jQuery (plugin only)

Sets the value of the selectmenu option associated with the specified optionName.
  • optionName
    Type: String
    The name of the option to set.
  • value
    Type: Object
    A value to set for the option.
Code examples:

Invoke the method:

1
$( ".selector" ).selectmenu( "option", "disabled", true );

option( options )Returns: jQuery (plugin only)

Sets one or more options for the selectmenu.
  • options
    Type: Object
    A map of option-value pairs to set.
Code examples:

Invoke the method:

1
$( ".selector" ).selectmenu( "option", { disabled: true } );

refresh()Returns: jQuery (plugin only)

update the custom select.

This is used to update the custom select to reflect the native select element's value. If the number of options in the select are different than the number of items in the custom menu, it'll rebuild the custom menu.

  • This signature does not accept any arguments.
Code examples:

Invoke the method:

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

refresh( option )Returns: jQuery (plugin only)

update the custom select.

This is used to update the custom select to reflect the native select element's value. If the number of options in the select are different than the number of items in the custom menu, it'll rebuild the custom menu. If you pass a true argument you can force the rebuild to happen.

  • option
    Type: Boolean
    to force a rebuild
Code examples:

Invoke the method:

1
$( ".selector" ).selectmenu( "refresh", true );

Events

create( event, ui )Type: selectcreate

Triggered when the selectmenu is created.

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

Code examples:

Initialize the selectmenu with the create callback specified:

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

Bind an event listener to the selectcreate event:

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

Example:

A basic example of a simple native select

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>selectmenu demo</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
</head>
<body>
<div data-role="page" id="page1">
<div data-role="header">
<h1>jQuery Mobile Example</h1>
</div>
<div role="main" class="ui-content">
<label for="select-choice-0" class="select">Shipping method:</label>
<select name="select-choice-0" id="select-choice-0">
<option value="standard">Standard: 7 day</option>
<option value="rush">Rush: 3 days</option>
<option value="express">Express: next day</option>
<option value="overnight">Overnight</option>
</select>
</div>
</div>
</body>
</html>

Demo: