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
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 radiobuttons in a container with the data-role="fieldcontain"
attribute to help visually group it in a longer form.
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.
Options
mini
false
This option is also exposed as a data attribute: data-mini="true"
.
Initialize the checkboxradio with the mini
option specified:
1
2
3
|
|
Get or set the mini
option, after initialization:
1
2
3
4
5
|
|
theme
null, inherited from parent
Possible values: swatch letter (a-z).
This option is also exposed as a data attribute: data-theme="a"
.
Initialize the checkboxradio with the theme
option specified:
1
2
3
|
|
Get or set the theme
option, after initialization:
1
2
3
4
5
|
|
Methods
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
|
|
refresh()Returns: jQuery (plugin only)
update the checkboxradio.
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 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
|
|