The select menus are driven off native select elements, but the native selects are hidden from view and replaced with more style-friendly markup. The replacement buttons and menus are ARIA-enabled and are keyboard accessible on the desktop as well.
select
When clicked, if the menu has room it will appear as an overlay listbox, but if there are too many options to fit in the window without scrolling, the page content is wrapped in a div and hidden, and the menu is appended as a whole new page. This lets us take advantage of native scrolling while the menu is in use.
To add a select widget 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. Wrap them in a div with the data-role="fieldcontain" attribute to help visually group it in a longer form.
option
for
label
div
data-role="fieldcontain"
The framework will find all select elements and automatically enhance them into the custom select menus.
<div data-role="fieldcontain"> <label for="select-choice-1" class="select">Choose 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>
If there is a small number of options that will fit on the device's screen, it will appear as a small overlay with a pop transition.
If there is a select menu with too many options to show on the device's screen, the framework will automatically create a new "page" populated with a standard list view that contains all 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.
The custom select menus add the ability to theme the select and provide visual consistency across platforms In addition, it fixes over some missing functionality on certain platforms: optgroup support on Android, multi-select capability on WebOS, and adds an elegant way to handle placeholder values (explained below).
optgroup
However, there is overhead involved in parsing the native select to build a custom menu and if the number of selects or options within are fairly large, this can impact the performance of the page. By adding the data-native-menu="true" attribute to the select, the framework will use the browser's native select menu when the select button is clicked. Because this option doesn't use any of the custom menu parsing and menu generation logic, it is significantly faster than the custom menu version.
data-native-menu="true"
You can also programmatically set the select menu's configuration option to true in a callback bound to the mobileinit event to achieve the same effect. The following must be included in the page after jQuery is loaded but before jQuery Mobile is loaded.
mobileinit
$(document).bind('mobileinit',function(){ $.mobile.selectmenu.prototype.options.nativeMenu = true; });
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;
Here's a demo of various placeholder 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.
disabled
If a select menu contains optgroup elements, jQuery Mobile will create a divider & group items based on the label attribute's text:
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.
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 though data- attributes.