5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 <title>jQuery Mobile Docs - Radio Buttons</title>
7 <link rel="stylesheet" href="../../../css/themes/default/" />
8 <link rel="stylesheet" href="../../_assets/css/jqm-docs.css"/>
9 <script src="../../../js/jquery.js"></script>
10 <script src="../../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
11 <script src="../../_assets/js/jqm-docs.js"></script>
12 <script src="../../../js/"></script>
16 <div data-role="page" class="type-interior">
18 <div data-role="header" data-theme="f">
19 <h1>Radio buttons</h1>
20 <a href="../../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
21 </div><!-- /header -->
23 <div data-role="content">
24 <div class="content-primary">
26 <form action="#" method="get">
28 <h2>Radio buttons</h2>
30 <ul data-role="controlgroup" data-type="horizontal" class="localnav">
31 <li><a href="index.html" data-role="button" data-transition="fade" class="ui-btn-active">Basics</a></li>
32 <li><a href="options.html" data-role="button" data-transition="fade">Options</a></li>
33 <li><a href="methods.html" data-role="button" data-transition="fade">Methods</a></li>
34 <li><a href="events.html" data-role="button" data-transition="fade">Events</a></li>
37 <p>Radio buttons are used to provide a list of options where only a single items can be selected. Traditional desktop radio buttons are not optimized for touch input so in jQuery Mobile, we style the <code>label</code> 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.</p>
39 <p>Both the radio and 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. View the <a href="../../api/data-attributes.html">data- attribute reference</a> to see all the possible attributes you can add to radio buttons.</p>
41 <h2>Vertically grouped radio buttons</h2>
43 <p>To create a set of radio buttons, add an <code>input</code> with a <code>type="radio"</code> attribute and a corresponding <code>label</code>. Set the <code>for</code> attribute of the <code>label</code> to match the ID of the <code>input</code> so they are semantically associated.</p>
45 <p>Because radio buttons use the <code>label</code> element for the text displayed next to the checkbox form element, we recommend wrapping the radio buttons in a <code>fieldset</code> element that has a <code>legend</code> which acts as the title for the question.</p>
47 <p>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 <code> data-role="controlgroup"</code> attribute on the container.</p>
50 <strong><fieldset data-role="controlgroup">
51 <legend>Choose a pet:</legend></strong>
52 <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
53 <label for="radio-choice-1">Cat</label>
55 <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
56 <label for="radio-choice-2">Dog</label>
58 <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3" />
59 <label for="radio-choice-3">Hamster</label>
61 <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4" />
62 <label for="radio-choice-4">Lizard</label>
63 <strong></fieldset></strong>
67 <p>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.</p>
70 <fieldset data-role="controlgroup">
71 <legend>Choose a pet:</legend>
72 <input type="radio" name="radio-pet-1a" id="radio-pet-1a" value="choice-1" checked="checked" />
73 <label for="radio-pet-1a">Cat</label>
75 <input type="radio" name="radio-pet-1a" id="radio-pet-2a" value="choice-2" />
76 <label for="radio-pet-2a">Dog</label>
78 <input type="radio" name="radio-pet-1a" id="radio-pet-3a" value="choice-3" />
79 <label for="radio-pet-3a">Hamster</label>
81 <input type="radio" name="radio-pet-1a" id="radio-pet-4a" value="choice-4" />
82 <label for="radio-pet-4a">Lizard</label>
87 <strong><div data-role="fieldcontain">
88 </strong> <fieldset data-role="controlgroup">
89 <legend>Choose a pet:</legend>
90 <input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
91 <label for="radio-choice-1">Cat</label>
93 <input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
94 <label for="radio-choice-2">Dog</label>
96 <input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3" />
97 <label for="radio-choice-3">Hamster</label>
99 <input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4" />
100 <label for="radio-choice-4">Lizard</label>
103 </strong> </code></pre>
106 <p>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 <code> data-role="controlgroup"</code> attribute on the container.</p>
108 <div data-role="fieldcontain">
109 <fieldset data-role="controlgroup">
110 <legend>Choose a pet:</legend>
111 <input type="radio" name="radio-pet-1" id="radio-pet-1" value="choice-1" checked="checked" />
112 <label for="radio-pet-1">Cat</label>
114 <input type="radio" name="radio-pet-1" id="radio-pet-2" value="choice-2" />
115 <label for="radio-pet-2">Dog</label>
117 <input type="radio" name="radio-pet-1" id="radio-pet-3" value="choice-3" />
118 <label for="radio-pet-3">Hamster</label>
120 <input type="radio" name="radio-pet-1" id="radio-pet-4" value="choice-4" />
121 <label for="radio-pet-4">Lizard</label>
124 <h2>Horizontal radio button sets</h2>
126 <p>Radio buttons can also be used for grouped button sets only a single button can be selected at once, such as a view switcher control. To make a horizontal radio button set, add the <code> data-type="horizontal"</code> to the <code>fieldset</code>.</p>
129 <fieldset data-role="controlgroup" <strong>data-type="horizontal"</strong> >
132 <div data-role="fieldcontain">
133 <fieldset data-role="controlgroup" data-type="horizontal">
134 <legend>Layout view:</legend>
135 <input type="radio" name="radio-view" id="radio-view-a" value="list" />
136 <label for="radio-view-a">List</label>
137 <input type="radio" name="radio-view" id="radio-view-b" value="grid" />
138 <label for="radio-view-b">Grid</label>
139 <input type="radio" name="radio-view" id="radio-view-c" value="gallery" />
140 <label for="radio-view-c">Gallery</label>
144 <p>The framework will float the labels so they sit side-by-side on a line, hides the radio button icons and only round the left and right edges of the group.</p>
153 </div><!--/content-primary -->
155 <div class="content-secondary">
157 <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
159 <h3>More in this section</h3>
161 <ul data-role="listview" data-theme="c" data-dividertheme="d">
163 <li data-role="list-divider">Form elements</li>
164 <li><a href="../docs-forms.html">Form basics</a></li>
165 <li><a href="../forms-all.html">Form element gallery</a></li>
166 <li><a href="../textinputs/index.html">Text inputs</a></li>
167 <li><a href="../search/">Search input</a></li>
168 <li><a href="../slider/">Slider</a></li>
169 <li><a href="../switch/">Flip toggle switch</a></li>
170 <li data-theme="a"><a href="index.html">Radio buttons</a></li>
171 <li><a href="../checkboxes/">Checkboxes</a></li>
172 <li><a href="../selects/">Select menus</a></li>
173 <li><a href="../forms-themes.html">Theming forms</a></li>
174 <li><a href="../forms-all-native.html">Native form elements</a></li>
175 <li><a href="../forms-sample.html">Submitting forms</a></li>
182 </div><!-- /content -->
184 <div data-role="footer" class="footer-docs" data-theme="c">
185 <p>© 2011 The jQuery Project</p>