Form elements
All form widgets begin with native form elements with rich HTML semantics that are enhanced to make them more attractive and finger-friendly.
Markup conventions
When constructing forms to be used in jQuery Mobile, most of the standard guidelines used to create forms that submit via ordinary HTTP POST or GET still apply. Additionally, the id
attributes of form controls need to be not only unique on a given page, but also unique across the pages in a site. This is because jQuery Mobile's single-page navigation model allows many different "pages" to be present in the DOM at the same time. You must be careful to use unique id
attributes so there will be only one of each in the DOM. Be sure to pair them properly with label
elements via the for
attribute.
Buttons
Buttons are used within a wide range of other plugins. The button markup is flexible and can be created from links or form buttons. Learn more about button markup and input buttons.
Inline buttons
Horizontal grouped buttons
Sliders
Sliders are used to enter numeric values along a numeric continuum by dragging a handle or entering in a value. Learn more about sliders.
Range slider
Range sliders offer two handles to set a min and max value along a numeric continuum. Learn more about range sliders.
Flip switch
Flip switches are used for boolean style inputs like true/false or on/off in a compact UI element. Learn more about flip switches.
Checkboxes
Checkboxes are used to provide a list of options where more than one can be selected. Learn more about checkboxes.
Radio buttons
Radio buttons are used to provide a list of options where only a single option can be selected. Learn more about radiobuttons.
Selects
The select menu is used to offer a list of options in a compact picker. Ours is based on a native select element, which is hidden from view and replaced with a custom-styled select button. Tapping it opens the native menu or a custom styled version. Learn more about selects.
Text inputs & Textareas
Text inputs and textareas are coded with standard HTML elements, then enhanced by jQuery Mobile to make them more attractive and useable on a mobile device. Learn more about text inputs and textareas.
See the form gallery for more form element examples.
Accessibly hidden labels
For the sake of accessibility, jQuery Mobile requires that all form elements be paired with a meaningful label
. To hide labels in a way that leaves them visible to assistive technologies — for example, when letting an element's placeholder
attribute serve as a label — apply the helper class ui-hidden-accessible
to the label. View more examples of accessibly hidden labels.
Disabling form elements
All jQuery Mobile widgets can be disabled in the markup by adding the standard disabled
attribute to the element, just like you would with native controls. To dynamically disable or enable them, each form widget also has standard disable
and enable
methods that are documented with each form widget. View more examples of disabled form elements.
Note that you can disable buttons created from button
or input
-based markup, but not links with a role of button. Links don't have a parallel disabled feature in HTML, but 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.
Fieldcontainer groupings
To improve the styling of labels and form elements on wider screens, wrap a div
or fieldset
with the class="ui-field-contain"
attribute around each label/form element. This framework aligns the input and associated label side-by-side, and breaks to stacked block-level elements below ~448px. The framework will also add a thin bottom border to act as a field separator. See more examples of fieldcontainer groupings.
Auto-initialization of form elements
By default, jQuery Mobile will automatically enhance certain native form controls into rich touch-friendly components. This is handled internally by finding form elements by tag name and running a plugin method on them. For instance, a select
element will be found and initialized with the "selectmenu" plugin, while an input
element with a type="checkbox"
will be enhanced with the "checkboxradio" plugin. Once initialized, you can address these enhanced components programmatically through their jQuery UI widget API methods. See options, methods, and events listed on each form plugin's documentation page for details.
Initializing groups of dynamically-injected form elements
If you should generate new markup client-side or load in content via Ajax and inject it into a page, you can trigger the create
event to handle the auto-initialization for all the plugins contained within the new markup. This can be triggered on any element (even the page div itself), saving you the task of manually initializing each plugin (see below).
For example, if a block of HTML markup (say a login form) was loaded in through Ajax, trigger the create event to automatically transform all the widgets it contains (inputs and buttons in this case) into the enhanced versions. The code for this scenario would be:
$( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
Refreshing form elements
In jQuery Mobile, some enhanced form controls are simply styled (inputs), but others are custom controls (selects, sliders) built from, and kept in sync with, the native control. To programmatically update a form control with JavaScript, first manipulate the native control, then use the refresh
method to tell the enhanced control to update itself to match the new state. Here are some examples of how to update common form controls, then call the refresh
method:
Checkboxes:
$( "input[type='checkbox']" ).prop( "checked", true ).checkboxradio( "refresh" );
Radios:
$( "input[type='radio']" ).prop( "checked", true ).checkboxradio( "refresh" );
Selects:
var myselect = $( "#selectfoo" );
myselect[0].selectedIndex = 3;
myselect.selectmenu( "refresh" );
Sliders:
$( "input[type='range']" ).val( 60 ).slider( "refresh" );
Flip switches:
They use the slider widget.
var myswitch = $( "#selectbar" );
myswitch[ 0 ].selectedIndex = 1;
myswitch.slider( "refresh" );
Preventing auto-initialization of form elements
If you'd prefer that a particular form control be left untouched by jQuery Mobile, simply give that element the attribute data-role="none"
. For example:
<label for="foo">
<select name="foo" id="foo" data-role="none">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
If you'd like to prevent auto-initialization without adding attributes to your markup, you can customize the selector that is used for preventing auto-initialization by setting the page plugin's keepNative
option (which defaults to [data-role="none"]
). Be sure to configure this option inside an event handler bound to the mobileinit
event, so that it applies to the first page as well as subsequent pages that are loaded.
$( document ).bind( "mobileinit", function() {
$.mobile.page.prototype.options.keepNative = "select, input.foo, textarea.bar";
});
Alternately you can use the data-enhance="false"
data attribute on a parent element with $.mobile.ignoreContentEnabled
set to true. Beware though, this will incur a performance penalty for each and every element in the page that would otherwise be enhanced as jQuery Mobile must traverse the set of parents to look for those elements.
One special case is that of selects. The above sample will prevent any and all augmentation from taking place on select elements in the page if select
is included. If you wish to retain the native performance and appearance of the menu itself and benefit from the visual augmentation of the select button by jQuery Mobile, you can set $.mobile.selectmenu.prototype.options.nativeMenu
to true in a mobileinit
callback as a global setting or use data-native-menu="true"
on a case by case basis.
File Inputs + Ajax
Using a multipart form with a file input is not supported by Ajax. In this case you should decorate the parent form with data-ajax="false"
to ensure the form is submitted properly to the server.