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.
$(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.
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"
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:
$.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:
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: