Forms

Home

jQuery Mobile provides a complete set of finger-friendly form elements that are based on native HTML form elements.

Form structure

All forms should be wrapped in a form tag that has an action and method that will handle the form data processing on the server.

<form action="form.php" method="post"> ... </form>

Markup Conventions

When constructing forms to be used in jQuery Mobile, most of the standard guidelines used to create forms that submit via normal HTTP post or get still apply. However, one thing to keep in mind is that 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, so you must be careful to use unique id attributes so there will be only one of each in the DOM (and of course, be sure to pair them properly with label elements via the for attribute).

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, so 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 documentation on available methods here: Form Plugin Methods).

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>

Or, 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";
});
		

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, look/feel of the menu itself and benefit from the visual augmentation of the select button by jQuery Mobile you can set $.mobile.nativeSelectMenu to true in a mobileinit callback as a global setting or use data-native="true" on a case by case basis.

Dynamic form layout

In jQuery Mobile, all form elements are designed to be a flexible width so they will comfortably fit the width of any mobile device. One optimization built into the framework is that we present labels and form elements differently based on screen width.

If a page is fairly narrow (~480px), the labels are styled as block-level elements so they will stack on top of the form element to save horizontal space.

On wider screens, the labels and form elements are styled to be on the same line in a 2-column layout to take advantage of the screen real estate.

Field containers

To improve the styling to labels and form elements on wider screens, we recommend wrapping a div or fieldset with the data-role="fieldcontain" attribute around each label/form element. The framework will add a thin vertical bottom border on this container to act as a field separator and visually align the label and form elements for quick scanning.


<div data-role="fieldcontain">
	...label/input code goes here...
</div>

Refreshing form elements

Every form control -- where applicable -- has a refresh method. Here are some examples:

Checkboxes:

$("input[type='checkbox']").attr("checked",true).checkboxradio("refresh");

Radios:

$("input[type='radio']").attr("checked",true).checkboxradio("refresh");

Selects:

var myselect = $("select#foo");
myselect[0].selectedIndex = 3;
myselect.selectmenu("refresh");

Sliders:

$("input[type=range]").val(60).slider("refresh");

Flip switches (they use slider):

var myswitch = $("select#bar");
myswitch[0].selectedIndex = 1;
myswitch .slider("refresh");

We're considering adding a refresh method to forms to let the framework refresh all the individual elemnts withing but that is a future enhancement.