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.
label
The radio 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. View the data- attribute reference to see all the possible attributes you can add to 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.
input
type="radio"
for
id
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.
fieldset
legend
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.
data-role="controlgroup"
<fieldset data-role="controlgroup"> <legend>Choose a pet:</legend> <input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" checked="checked" /> <label for="radio-choice-1">Cat</label> <input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2" /> <label for="radio-choice-2">Dog</label> <input type="radio" name="radio-choice" id="radio-choice-3" value="choice-3" /> <label for="radio-choice-3">Hamster</label> <input type="radio" name="radio-choice" id="radio-choice-4" value="choice-4" /> <label for="radio-choice-4">Lizard</label> </fieldset>
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.
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.
data-mini="true"
<fieldset data-role="controlgroup" data-mini="true"> <input type="radio" name="radio-mini" id="radio-mini-1" value="choice-1" checked="checked" /> <label for="radio-mini-1">Credit</label> <input type="radio" name="radio-mini" id="radio-mini-2" value="choice-2" /> <label for="radio-mini-2">Debit</label> <input type="radio" name="radio-mini" id="radio-mini-3" value="choice-3" /> <label for="radio-mini-3">Cash</label> </fieldset>
This will produce a radio button that is not as tall as the standard version and has a smaller text size.
Optionally wrap the radiobuttons in a container with the data-role="fieldcontain" attribute to help visually group it in a longer form.
data-role="fieldcontain"
<div data-role="fieldcontain"> <fieldset data-role="controlgroup"> <legend>Choose a pet:</legend> <input type="radio" name="radio-choice-2" id="radio-choice-21" value="choice-1" checked="checked" /> <label for="radio-choice-21">Cat</label> <input type="radio" name="radio-choice-2" id="radio-choice-22" value="choice-2" /> <label for="radio-choice-22">Dog</label> <input type="radio" name="radio-choice-2" id="radio-choice-23" value="choice-3" /> <label for="radio-choice-23">Hamster</label> <input type="radio" name="radio-choice-2" id="radio-choice-24" value="choice-4" /> <label for="radio-choice-24">Lizard</label> </fieldset> </div>
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.
data-type="horizontal"
<fieldset data-role="controlgroup" data-type="horizontal" >
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.