# 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).

## Hiding labels accessibly

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 itself:

`

    <label for="username" class="ui-hidden-accessible">Username:</label>
    <input type="text" name="username" id="username" value="" placeholder="Username"/>

`

To hide labels within a field container and adjust the layout accordingly, add the class `ui-hide-label` to the field container as in the following:

`

    <div data-role="fieldcontain" class="ui-hide-label">
    	<label for="username">Username:</label>
    	<input type="text" name="username" id="username" value="" placeholder="Username"/>
    </div>

`

Both of the above examples will render as:

Username:

While the label will no longer be visible, it will be available to assisitive technologies such as screen readers.

## 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. Each form widget also has standard `disable` and `enable` methods that are documented with each form widget. Here are a few examples of disabled widgets:

Text Input:
Gender: Female Male
Server status:
Max bandwidth:
Shipping:

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.

## 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. This framework aligns the input and associated label side-by-side, and breaks to stacked block-level elements below ~480px. The framework will also add a thin bottom border to act as a field separator.

For example:

    <div data-role="fieldcontain">
    <label for="name">Text Input:</label>
    <input type="text" name="name" id="name" value="" />
    </div>

Will render as:

Text Input:

For additional examples, see the form elements gallery

## 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 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 slider):

`

    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>

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.

### More in this section

- Form elements
- Form basics
- Form element gallery
- Text inputs
- Search inputs
- Slider
- Flip toggle switch
- Radio buttons
- Checkboxes
- Select menus
- Theming forms
- Native form elements
- Submitting forms