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. View the data- attribute reference to see all the possible attributes you can add to selects.
select
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.
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.
option
for
label
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.
data-role
data-role="none"
<label for="select-choice-0" class="select">Shipping method:</label> <select name="select-choice-0" 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>
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.
Optionally wrap the selects in a container with the data-role="fieldcontain" attribute to help visually group it in a longer form.
data-role="fieldcontain"
<div data-role="fieldcontain"> <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:
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, we recommend wrapping the selects in a fieldset element that has a legend which acts as the combined label for the grouped inputs.
fieldset
legend
Lastly, one needs to wrap the fieldset in a div with data-role="controlgroup" attribute, so it can be styled as a group.
div
data-role="controlgroup"
<div data-role="fieldcontain"> <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>
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. Note that browsers without support for display: inline-block; will group the selects vertically, as above.
data-type="horizontal"
display: inline-block;
<fieldset data-role="controlgroup" data-type="horizontal">
the framework builds a custom menu based on the select element's list of options. We recommend using a custom menu when multiple selections are required, as , 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.
optgroup
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.
data-native-menu="false"
nativeMenu
false
mobileinit
$(document).bind('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 "page" populated with a standard list view 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.
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.
disabled
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:
data-placeholder="true"
You can disable this feature through the selectmenu plugin's hidePlaceholderMenuItems option, like this:
hidePlaceholderMenuItems
$.mobile.selectmenu.prototype.options.hidePlaceholderMenuItems = false;
Examples of various placeholder options:
If the multiple attribute is present in your markup, jQuery Mobile will enhance the element with a few extra considerations:
multiple
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.
If a select menu contains optgroup elements, jQuery Mobile will create a divider & group items based on the label attribute's text:
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:
data-
The data-overlay-theme attribute can be added a select element to set the color of the overlay layer that 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.
data-overlay-theme
The select menu plugin will auto initialize on any page that contains a select menu, no 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:
$('select').selectmenu();