Flip Toggle Switch

Home

Flip toggle switch

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.

To create a flip toggle, start with a select 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 data- attribute reference to see all the possible attributes you can add to flip switches.

Set the for attribute of the label to match the ID of the input so they are semantically associated. It's possible to accessibly hide the label if it's not desired in the page layout, but we require that it is present in the markup for semantic and accessibility reasons.

	
<label for="flip-a">Select slider:</label>
<select name="slider" id="flip-a" data-role="slider">
	<option value="off">Off</option>
	<option value="on">On</option>
</select> 

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 stacks the label on a separate line.

Optionally wrap the switch markup in a container with the data-role="fieldcontain" attribute to help visually group it in a longer form.

	
<div data-role="fieldcontain">
<label for="flip-b">Flip switch:</label>
	<select name="slider" id="flip-b" data-role="slider">
		<option value="no">No</option>
		<option value="yes">Yes</option>
	</select> 
</div>

The flip toggle switch is now displayed like this:

Theming the flip switch

Like all form elements, this widget will automatically inherit the theme from it's parent container. TO choose a specific theme color swatch, specify the data-theme attribute on the select and specify a swatch letter.

	
<div data-role="fieldcontain">
	<label for="flip-c">Flip switch:</label>
	<select name="slider" id="flip-c" data-role="slider" data-theme="a">
		<option value="no">No</option>
		<option value="yes">Yes</option>
	</select> 
</div>

This results in a switch with the A swatch colors for the handle. Note that the lefthand "on" state gets the active state color.

Here is a E swatch variation:

Calling the switch plugin

This plugin will auto initialize on any page that contains a select with the data-role="slider" attribute. However, if needed you can directly call the slider plugin on any selector, just like any jQuery plugin:


$('select').slider();