5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 <title>jQuery Mobile Docs - Switch</title>
7 <link rel="stylesheet" href="../../../css/themes/default/jquery.mobile.css" />
8 <link rel="stylesheet" href="../../_assets/css/jqm-docs.css"/>
9 <script src="../../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
10 <script src="../../../js/jquery.js"></script>
11 <script src="../../../docs/_assets/js/jqm-docs.js"></script>
12 <script src="../../../js/"></script>
17 <div data-role="page" class="type-interior">
19 <div data-role="header" data-theme="f">
20 <h1>Flip Toggle Switch</h1>
21 <a href="../../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
22 <a href="../../nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</a>
23 </div><!-- /header -->
25 <div data-role="content">
26 <div class="content-primary">
28 <form action="#" method="get">
29 <h2>Flip toggle switch</h2>
31 <ul data-role="controlgroup" data-type="horizontal" class="localnav">
32 <li><a href="index.html" data-role="button" data-transition="fade" class="ui-btn-active">Basics</a></li>
33 <li><a href="options.html" data-role="button" data-transition="fade">Options</a></li>
34 <li><a href="methods.html" data-role="button" data-transition="fade">Methods</a></li>
35 <li><a href="events.html" data-role="button" data-transition="fade">Events</a></li>
38 <p>A binary "flip" switch is a common UI element on mobile devices that is used for binary on/off or true/false data input. You can either drag the flip handle like a slider or tap one side of the switch.</p>
40 <p>To create a flip toggle, start with a <code>select</code> with two options. The first option will be styled as the "on" state switch and the second will be styled as the "off" state so write your options accordingly. View the <a href="../../api/data-attributes.html">data- attribute reference</a> to see all the possible attributes you can add to flip switches.</p>
42 <p>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. It's possible to <a href="../docs-forms.html">accessibly hide the label</a> if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.</p>
45 <label for="flip-a">Select slider:</label>
46 <select name="slider" id="flip-a" data-role="slider">
47 <option value="off">Off</option>
48 <option value="on">On</option>
52 <p>This will produce a basic flip toggle switch input. The default styles set the width of the switch to 100% of the parent container and stack the label on a separate line.</p>
54 <label for="flip-a">Flip switch:</label>
55 <select name="slider" id="flip-a" data-role="slider">
56 <option value="off">Off</option>
57 <option value="on">On</option>
61 <h2>Longer Labels</h2>
62 <p>The control is proportionally scaled, so to use longer labels one can just add a line of CSS setting the switch to the desired width. For example, given the following markup:</p>
64 <div class="containing-element">
65 <label for="flip-min">Flip switch:</label>
66 <select name="slider" id="flip-min" data-role="slider">
67 <option value="off">Switch Off</option>
68 <option value="on">Switch On</option>
73 <p><code>.containing-element .ui-slider-switch { width: 9em }</code> will produce:</p>
75 <style type="text/css" media="screen">
76 .containing-element .ui-slider-switch { width: 9em; }
79 <div class="containing-element">
80 <label for="flip-min">Flip switch:</label>
81 <select name="slider" id="flip-min" data-role="slider">
82 <option value="off">Switch Off</option>
83 <option value="on">Switch On</option>
87 <p>As some default styles hinge on fieldcontains, note that you may have to ensure that custom styles apply to switches within fieldcontains by using <code>.ui-field-contain div.ui-slider-switch { width: […]; }</code>.</p>
91 <p>For a more compact version that is useful in toolbars and tight spaces, add the <code>data-mini="true"</code> attribute to the element to create a <a href="../forms-all-mini.html">mini version</a>. </p>
94 <label for="flip-a">Select slider:</label>
95 <select name="slider" id="flip-a" data-role="slider" <strong>data-mini="true"</strong>>
96 <option value="off">Off</option>
97 <option value="on">On</option>
101 <p>This will produce a flip switch that is not as tall as the standard version and has a smaller text size.</p>
102 <label for="flip-min">Flip switch:</label>
103 <select name="slider" id="flip-min" data-role="slider" data-mini="true">
104 <option value="off">Off</option>
105 <option value="on">On</option>
109 <h2>Field containers</h2>
110 <p>Optionally wrap the switch markup in a container with the <code>data-role="fieldcontain"</code> attribute to help visually group it in a longer form.</p>
113 <strong><div data-role="fieldcontain">
114 </strong><label for="flip-b">Flip switch:</label>
115 <select name="slider" id="flip-b" data-role="slider">
116 <option value="no">No</option>
117 <option value="yes">Yes</option>
120 </strong></code></pre>
121 <p>The flip toggle switch is now displayed like this:</p>
122 <div data-role="fieldcontain">
123 <label for="flip-b">Flip switch:</label>
124 <select name="slider" id="flip-b" data-role="slider">
125 <option value="nope">Nope</option>
126 <option value="yep">Yep</option>
131 <h2>Theming the flip switch</h2>
133 <p>Like all form elements, this widget will automatically inherit the theme from its parent container. To choose a specific theme color swatch, specify the <code>data-theme</code> attribute on the select and specify a swatch letter.</p>
136 <div data-role="fieldcontain">
137 <label for="flip-c">Flip switch:</label>
138 <select name="slider" id="flip-c" data-role="slider" data-theme="a">
139 <option value="no">No</option>
140 <option value="yes">Yes</option>
144 <p>This results in a switch with the A swatch colors for the handle. Note that the lefthand "on" state gets the active state color.</p>
145 <div data-role="fieldcontain">
146 <label for="flip-c">Flip switch:</label>
147 <select name="slider" id="flip-c" data-role="slider" data-theme="a">
148 <option value="hate">Hate</option>
149 <option value="love">Love</option>
153 <p>Here is a E swatch variation:</p>
154 <div data-role="fieldcontain">
155 <label for="flip-d">Flip switch:</label>
156 <select name="slider" id="flip-d" data-role="slider" data-theme="e">
157 <option value="no">No</option>
158 <option value="yes">Yes</option>
162 <h2>Calling the switch plugin</h2>
164 <p>This plugin will auto-initialize on any page that contains a <code>select</code> with the <code>data-role="slider"</code> attribute. However, if needed you can directly call the <code>slider</code> plugin on any selector, just like any jQuery plugin:</p>
166 $('select').slider();
170 </div><!--/content-primary -->
172 <div class="content-secondary">
174 <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
176 <h3>More in this section</h3>
178 <ul data-role="listview" data-theme="c" data-dividertheme="d">
180 <li data-role="list-divider">Form elements</li>
181 <li><a href="../docs-forms.html">Form basics</a></li>
182 <li><a href="../forms-all.html">Form element gallery</a></li>
183 <li><a href="../forms-all-mini.html">Mini form element gallery</a></li>
184 <li><a href="../textinputs/index.html">Text inputs</a></li>
185 <li><a href="../search/">Search input</a></li>
186 <li><a href="../slider/">Slider</a></li>
187 <li data-theme="a"><a href="index.html">Flip toggle switch</a></li>
188 <li><a href="../radiobuttons/">Radio buttons</a></li>
189 <li><a href="../checkboxes/">Checkboxes</a></li>
190 <li><a href="../selects/">Select menus</a></li>
191 <li><a href="../forms-themes.html">Theming forms</a></li>
192 <li><a href="../forms-all-native.html">Native form elements</a></li>
193 <li><a href="../forms-sample.html">Submitting forms</a></li>
200 </div><!-- /content -->
202 <div data-role="footer" class="footer-docs" data-theme="c">
203 <p>© 2011-12 The jQuery Foundation</p>