Form elements - jQuery Mobile Demos

##

Demos

Menu Search

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

Link button

Inline buttons

Inline + icon Mini + theme icon only button

Horizontal grouped buttons

Add Delete More

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

Slider:
Slider with fill and step of 50:
Slider with fill, mini, track theme:

## Range slider

Range sliders offer two handles to set a min and max value along a numeric continuum. Learn more about range sliders.

Rangeslider: Rangeslider:
Mini rangeslider: Mini rangeslider:

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

Flip switch:
Mini flip switch:

## Checkboxes

Checkboxes are used to provide a list of options where more than one can be selected. Learn more about checkboxes.

Checkboxes, vertical controlgroup: Cheetos Doritos Fritos Sun Chips
Checkboxes, mini, horizontal controlgroup: b i u

## Radio buttons

Radio buttons are used to provide a list of options where only a single option can be selected. Learn more about radiobuttons.

Radio buttons, vertical controlgroup: Cat Dog Hamster Lizard
Radio buttons, mini, horizontal controlgroup: List Grid Gallery

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

Select, native menu
Mini select, inline
Custom select menu:
Multi-select with optgroups, custom icon and position:

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

Text input:
Textarea:
Number + [0-9]* pattern:
Date:
Tel:
Search Input:
File:
Password:

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.

Text Input:

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

Text Input:

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.

Text Input:
Native select:

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

- Home
- Introduction
- Buttons
- Button widget

- ### Checkboxradio widget click to expand contents

  - Checkboxes
  - Radio buttons

- ### Collapsible (set) widget click to expand contents

  - Collapsible
  - Dynamic collapsibles
  - Collapsible set

- ### Controlgroup widget click to expand contents

  - Controlgroup
  - Dynamic controlgroups
- Datepicker

- ### Events click to expand contents

  - Swipe list items
  - Swipe page navigation
- Filterable widget
- Flipswitch widget

- ### Forms click to expand contents

  - Forms
  - Hide labels
  - Field containers
  - Forms disabled
  - Forms gallery

- ### Grids click to expand contents

  - Grids
  - Buttons in grids
  - Custom responsive grids
- Grouping and dividing content

- ### Icons click to expand contents

  - Icons
  - Grunticon loader

- ### Listview widget click to expand contents

  - Listview
  - Listview autocomplete
  - Listview autocomplete remote data
  - Listview autodividers linkbar
  - Listview autodividers selector
  - Nested Listviews
  - Listview collapsible list items (flat)
  - Listview collapsible list items (indented)
  - Listview responsive grid
- Loader widget
- Navbar widget

- ### Navigation click to expand contents

  - Navigation
  - Linking pages
  - Hash processing demo
  - Page Navigation Events

- ### Pages click to expand contents

  - Pages
  - Single page
  - Multi-page template
  - Dialog page

- ### Panel widget click to expand contents

  - Panel
  - External panels
  - Fixed panels
  - Panels responsive
  - Custom panel style
  - Panel open on swipe
  - Panel external and internal

- ### Popup widget click to expand contents

  - Popup
  - Popup alignment
  - Popup arrow size
  - Dynamic popups
  - Popups with iframes
  - Popup image scaling
  - Popup outside multi-page
- Rangeslider widget
- Responsive Web Design

- ### Selectmenu widget click to expand contents

  - Selectmenu
  - Custom select menu
  - Custom select menu with filter

- ### Slider widget click to expand contents

  - Slider
  - Slider flip toggle switch
  - Slider tooltip

- ### Table widget click to expand contents

  - Table Column Toggle
  - Table Column Toggle demo
  - Table Column Toggle heading groups
  - Table Column Toggle options
  - Table Reflow
  - Table Reflow heading groups
  - Table Reflow stripes and strokes
  - Table Reflow custom styles
- Tabs widget
- Textinput widget

- ### Theming click to expand contents

  - Default theme
  - Classic theme

- ### Toolbar widget click to expand contents

  - Toolbar
  - Dynamic toolbars
  - External toolbars
  - Fixed toolbars
  - Fullscreen toolbars
  - Fixed external toolbars
  - Persistent toolbars
  - Ajax optimized toolbars
  - Form in toolbar
- Transitions

- ### 3rd party API demos click to expand contents

  - Backbone RequireJS
  - Google Maps geolocation
  - Google Maps list toggle

- Home
- Introduction
- Buttons
- Button widget

- ### Checkboxradio widget click to expand contents

  - Checkboxes
  - Radio buttons

- ### Collapsible (set) widget click to expand contents

  - Collapsible
  - Dynamic collapsibles
  - Collapsible set

- ### Controlgroup widget click to expand contents

  - Controlgroup
  - Dynamic controlgroups
- Datepicker

- ### Events click to expand contents

  - Swipe list items
  - Swipe page navigation
- Filterable widget
- Flipswitch widget

- ### Forms click to expand contents

  - Forms
  - Hide labels
  - Field containers
  - Forms disabled
  - Forms gallery

- ### Grids click to expand contents

  - Grids
  - Buttons in grids
  - Custom responsive grids
- Grouping and dividing content

- ### Icons click to expand contents

  - Icons
  - Grunticon loader

- ### Listview widget click to expand contents

  - Listview
  - Listview autocomplete
  - Listview autocomplete remote data
  - Listview autodividers linkbar
  - Listview autodividers selector
  - Nested Listviews
  - Listview collapsible list items (flat)
  - Listview collapsible list items (indented)
  - Listview responsive grid
- Loader widget
- Navbar widget

- ### Navigation click to expand contents

  - Navigation
  - Linking pages
  - Hash processing demo
  - Page Navigation Events

- ### Pages click to expand contents

  - Pages
  - Single page
  - Multi-page template
  - Dialog page

- ### Panel widget click to expand contents

  - Panel
  - External panels
  - Fixed panels
  - Panels responsive
  - Custom panel style
  - Panel open on swipe
  - Panel external and internal

- ### Popup widget click to expand contents

  - Popup
  - Popup alignment
  - Popup arrow size
  - Dynamic popups
  - Popups with iframes
  - Popup image scaling
  - Popup outside multi-page
- Rangeslider widget
- Responsive Web Design

- ### Selectmenu widget click to expand contents

  - Selectmenu
  - Custom select menu
  - Custom select menu with filter

- ### Slider widget click to expand contents

  - Slider
  - Slider flip toggle switch
  - Slider tooltip

- ### Table widget click to expand contents

  - Table Column Toggle
  - Table Column Toggle demo
  - Table Column Toggle heading groups
  - Table Column Toggle options
  - Table Reflow
  - Table Reflow heading groups
  - Table Reflow stripes and strokes
  - Table Reflow custom styles
- Tabs widget
- Textinput widget

- ### Theming click to expand contents

  - Default theme
  - Classic theme

- ### Toolbar widget click to expand contents

  - Toolbar
  - Dynamic toolbars
  - External toolbars
  - Fixed toolbars
  - Fullscreen toolbars
  - Fixed external toolbars
  - Persistent toolbars
  - Ajax optimized toolbars
  - Form in toolbar
- Transitions

- ### 3rd party API demos click to expand contents

  - Backbone RequireJS
  - Google Maps geolocation
  - Google Maps list toggle