Checkboxradio Widgetversion added: 1.0
Description: Creates a checkboxradio widget
Checkboxes
Checkboxes are used to provide a list of options where more than one can be selected. Traditional desktop checkboxes are not optimized for touch input so in jQuery Mobile, we style the label
for the checkboxes so they are larger and look clickable. A custom set of icons are added to the label to provide additional visual feedback.
The checkbox controls below use standard input/label markup, but are styled to be more touch-friendly. The styled control you see is actually the label element, which sits over the real input, so if images fail to load, you'll still have a functional control. In most browsers, clicking the label automatically triggers a click on the input, but we've had to trigger the update manually for a few mobile browsers that don't do this natively. On the desktop, these controls are keyboard and screen-reader accessible.
To create a single checkbox, add an input
with a type="checkbox"
attribute and a corresponding label
. If the input
isn't wrapped in its corresponding label
, be sure to set the for
attribute of the label
to match the id
of the input
so they are semantically associated.
1
2
3
4
|
|
The above snippets will produce two basic checkboxes. The default styles will set the width of the element to 100% of the parent container.
Mini version
For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true"
attribute to the element to create a mini version.
1
2
|
|
This will produce a checkbox that is not as tall as the standard version and has a smaller text size.
Field containers & Legends
Optionally wrap the checkboxes in a container with class ui-field-contain
to help visually group it in a longer form.
Note: The data-
attribute data-role="fieldcontain"
is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0. Add class ui-field-contain
instead.
Because checkboxes use the label
element for the text displayed next to the checkbox form element, we recommend wrapping the checkbox in a fieldset
element that has a legend
which acts as the title for the question. Add the data-role="controlgroup"
attribute to the fieldset
so it can be styled in a parallel way as text inputs, selects or other form elements.
1
2
3
4
5
6
7
|
|
Vertically grouped checkboxes
Typically, there are multiple checkboxes listed under a question title. To visually integrate multiple checkboxes into a grouped button set, the framework will automatically remove all margins between buttons and round only the top and bottom corners of the set if there is a data-role="controlgroup"
attribute on the fieldset
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
|
Horizontal toggle sets
Checkboxes can also be used for grouped button sets where more than one button can be selected at once, such as the bold, italic and underline button group seen in word processors. To make a horizontal button set, add the data-type="horizontal"
to the fieldset
.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|
The framework will float the labels so they sit side-by-side on a line, hide the checkbox icons and only round the left and right edges of the group.
Radio buttons
Radio buttons are used to provide a list of options where only a single item can be selected. Traditional desktop radio buttons are not optimized for touch input so jQuery Mobile styles the label
for the radio buttons so they are larger and look clickable. A custom set of icons are added to the label to provide additional visual feedback.
The radio button controls below use standard input/label markup, but are styled to be more touch-friendly. The styled control you see is actually the label element, which sits over the real input, so if images fail to load, you'll still have a functional control. In most browsers, clicking the label automatically triggers a click on the input, but we've had to trigger the update manually for a few mobile browsers that don't do this natively. On the desktop, these controls are keyboard and screen-reader accessible.
Vertically grouped radio buttons
To create a set of radio buttons, add an input with a type="radio" attribute and a corresponding label. Set the for attribute of the label to match the id of the input so they are semantically associated.
The label element is displayed next to the radio form element. Wrap the radio buttons in a fieldset element that has a legend which acts as the title for the question.
To visually integrate multiple radio buttons into a vertically grouped button set, the framework will automatically remove all margins between buttons and round only the top and bottom corners of the set if there is a data-role="controlgroup" attribute on the container.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
|
This will produce a vertically grouped radio button set. The default styles set the width of the button group to 100% of the parent container and stacks the label on a separate line.
Mini version
For a more compact version that is useful in toolbars and tight spaces, add the data-mini="true"
attribute to the element to create a mini version.
1
2
3
4
5
6
7
8
9
10
|
|
This will produce a radio button that is not as tall as the standard version and has a smaller text size.
Field containers
Optionally wrap the radio buttons in a container with class ui-field-contain
to help visually group it in a longer form.
Note: The data-
attribute data-role="fieldcontain"
is deprecated as of jQuery Mobile 1.4.0 and will be removed in 1.5.0. Add class ui-field-contain
instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
|
Horizontal radio button sets
Radio buttons can also be used for grouped button sets where only a single button can be selected at once, such as a view switcher control. To make a horizontal radio button set, add the data-type="horizontal"
to the fieldset
.
1
|
|
The labels float so they sit side-by-side on a line. The radio button icons are hidden and only the left and right edges of the group are rounded.
Providing pre-rendered markup
You can improve the load time of your page by providing the markup that the checkboxradio widget would normally create during its initialization.
By providing this markup yourself, and by indicating that you have done so by setting the attribute data-enhanced="true"
, you instruct the checkboxradio widget to skip these DOM manipulations during instantiation and to assume that the required DOM structure is already present.
When you provide such pre-rendered markup you must also set all the classes that the framework would normally set, and you must also set all data attributes whose values differ from the default to indicate that the pre-rendered markup reflects the non-default value of the corresponding widget option.
The checkboxradio widget wraps the input
element in a div
and prepends the label
element to the div
.
In the example below, pre-rendered markup for a checkbox is provided. The attribute data-corners="false"
is explicitly specified, since the absence of the ui-corner-all
class on the label
element indicates that the value of the "corners" widget option is to be false.
1
2
3
4
|
|
Options
defaults
false
true
indicates that other widgets options have default values and causes jQuery Mobile's widget autoenhancement code to omit the step where it retrieves option values from data attributes. This can improve startup time.
This option is also exposed as a data attribute: data-defaults="true"
.
Initialize the checkboxradio with the defaults
option specified:
1
2
3
|
|
Get or set the defaults
option, after initialization:
1
2
3
4
5
|
|
disabled
false
true
.
This option is also exposed as a data attribute: data-disabled="true"
.
Initialize the checkboxradio with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
enhanced
false
This option is also exposed as a data attribute: data-enhanced="true"
.
Initialize the checkboxradio with the enhanced
option specified:
1
2
3
|
|
Get or set the enhanced
option, after initialization:
1
2
3
4
5
|
|
iconpos
"left"
This option is also exposed as a data attribute: data-iconpos="right"
.
Initialize the checkboxradio with the iconpos
option specified:
1
2
3
|
|
Get or set the iconpos
option, after initialization:
1
2
3
4
5
|
|
initSelector
See below
The default initSelector
for the checkboxradio widget is:
1
|
|
Note: This option is deprecated in 1.4.0 and will be removed in 1.5.0.
As of jQuery Mobile 1.4.0, the initSelector
is no longer a widget option. Instead, it is declared directly on the widget prototype. Thus, you may specify a custom value by handling the mobileinit
event and overwriting the initSelector
on the prototype:
1
2
3
|
|
Note: Remember to attach the mobileinit
handler after you have loaded jQuery, but before you load jQuery Mobile, because the event is triggered as part of jQuery Mobile's loading process.
The value of this option is a jQuery selector string. The framework selects elements based on the value of this option and instantiates checkboxradio widgets on each of the resulting list of elements.
(version deprecated: 1.4.0)mini
null (false)
true
, this will display a more compact version of the checkboxradio that uses less vertical height by applying the ui-mini
class to the outermost element of the checkboxradio widget.
This option is also exposed as a data attribute: data-mini="true"
.
wrapperClass
null
div
around the native input
element generated by the framework. This option allows you to specify one or more space-separated class names to be added to the wrapper div
element by the framework.
This option is also exposed as a data attribute: data-wrapper-class="custom-class"
.
Initialize the checkboxradio with the wrapperClass
option specified:
1
2
3
|
|
Get or set the wrapperClass
option, after initialization:
1
2
3
4
5
|
|
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
option( optionName )Returns: Object
optionName
.-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
option( optionName, value )Returns: jQuery (plugin only)
optionName
.-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
refresh()Returns: jQuery (plugin only)
If you manipulate a checkboxradio via JavaScript, you must call the refresh method on it to update the visual styling.
- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
1
|
|
Invoke the refresh method after changing the checked
property:
1
|
|
Events
create( event, ui )Type: checkboxradiocreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the checkboxradio with the create callback specified:
1
2
3
|
|
Bind an event listener to the checkboxradiocreate event:
1
|
|
Examples:
A basic example of a checkbox in a fieldcontainer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|
Demo:
A basic example of vertically grouped radio buttons
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
|