Flipswitch Widgetversion added: 1.4
Description: Creates a flipswitch widget
Flip switches
The flip switch is an alternative look to the checkbox or the two-option select menu. It can be toggled by a click or a swipe.
To create a flip switch add the attribute data-role="flipswitch"
to a checkbox input
or to a select
which has two option
values.
Checkbox-based flipswitch
Use the following markup to create a flipswitch based on a checkbox input
:
<fieldset> <div data-role="fieldcontain"> <label for="checkbox-based-flipswitch">Checkbox-based:</label> <input type="checkbox" id="checkbox-based-flipswitch" data-role="flipswitch"> </div> </fieldset>The labels for the flipswitch are controlled by the
onText
and offText
options. Select-based flipswitch
You can also create a flipswitch based on a select
element:
<fieldset> <div data-role="fieldcontain"> <label for="select-based-flipswitch">Select-based:</label> <select id="select-based-flipswitch" data-role="flipswitch"> <option value="leave">Bye</option> <option value="arrive">Hi</option> </select> </div> </fieldset>The labels for the flipswitch are controlled by the labels of the
select
element's option
elements. The first option is used for the inactive, "off" state, and the second option is used for the active, "on" state. Custom labels
You can customize the labels displayed by the flipswitch widget either using the onText and offText options if the widget is based on a checkbox, or using the text inside the first two option
elements if the widget is based on a select
.
The location of the text inside the two labels is specified manually in the flipswitch widget's structural CSS. Thus, if you use labels that are longer than "On" and "Off", you may have to override the default text placement using the following custom CSS:
.ui-flipswitch .ui-btn.ui-flipswitch-on { text-indent: -2.6em; } .ui-flipswitch .ui-flipswitch-off { text-indent: 1em; }
Depending on your choice of labels, you may also have to provide custom CSS to override the default width for the flipswitch:
.ui-flipswitch { width: 5.875em; } .ui-flipswitch.ui-flipswitch-active { padding-left: 4em; width: 1.875em; } @media (min-width: 28em) { // Repeated from rule .ui-flipswitch above .ui-field-contain > label + .ui-flipswitch { width: 1.875em; } }
Providing pre-rendered markup
You can improve the load time of your page by providing the markup that the flipswitch widget would normally create during its initialization.
By providing this markup yourself, and by indicating that you have done so by setting the attribute data-enhanced="true"
, you instruct the flipswitch widget to skip these DOM manipulations during instantiation and to assume that the required DOM structure is already present.
When you provide such pre-rendered markup you must also set all the classes that the framework would normally set, and you must also set all data attributes whose values differ from the default to indicate that the pre-rendered markup reflects the non-default value of the corresponding widget option.
The flipswitch widget adds a wrapper div
around the input
. In addition to the original element, the wrapper also contains two span
elements where the two labels are stored. The first span
is styled as a button with the text for the active state appearing outside the button's bounding box to the left. To make the flipswitch reachable by tabbing, you can add the tabindex="1"
attribute to the first span
.
In the example below, pre-rendered markup for a flipswitch is provided. The attribute data-corners="false"
is explicitly specified, since the absence of the ui-corner-all
class on the wrapper element indicates that the "corners" widget option is to be false.
<div class="ui-flipswitch ui-shadow-inset ui-bar-inherit"> <span tabindex="1" class="ui-flipswitch-on ui-btn ui-shadow ui-btn-inherit">On</span> <span class="ui-flipswitch-off">Off</span> <input type="checkbox" data-role="flipswitch" data-enhanced="true" data-corners="false" name="flip-checkbox" class="ui-flipswitch-input"> </div>
Options
corners
true
This option is also exposed as a data attribute: data-corners="false"
.
Initialize the flipswitch with the corners
option specified:
$( ".selector" ).flipswitch({ corners: false });
Get or set the corners
option, after initialization:
// Getter var corners = $( ".selector" ).flipswitch( "option", "corners" ); // Setter $( ".selector" ).flipswitch( "option", "corners", false );
defaults
false
true
indicates that other widgets options have default values and causes jQuery Mobile's widget autoenhancement code to omit the step where it retrieves option values from data attributes. This can improve startup time. This option is also exposed as a data attribute: data-defaults="true"
.
Initialize the flipswitch with the defaults
option specified:
$( ".selector" ).flipswitch({ defaults: true });
Get or set the defaults
option, after initialization:
// Getter var defaults = $( ".selector" ).flipswitch( "option", "defaults" ); // Setter $( ".selector" ).flipswitch( "option", "defaults", true );
disabled
false
true
. This option is also exposed as a data attribute: data-disabled="true"
.
Initialize the flipswitch with the disabled
option specified:
$( ".selector" ).flipswitch({ disabled: true });
Get or set the disabled
option, after initialization:
// Getter var disabled = $( ".selector" ).flipswitch( "option", "disabled" ); // Setter $( ".selector" ).flipswitch( "option", "disabled", true );
enhanced
false
This option is also exposed as a data attribute: data-enhanced="true"
.
Initialize the flipswitch with the enhanced
option specified:
$( ".selector" ).flipswitch({ enhanced: true });
Get or set the enhanced
option, after initialization:
// Getter var enhanced = $( ".selector" ).flipswitch( "option", "enhanced" ); // Setter $( ".selector" ).flipswitch( "option", "enhanced", true );
mini
null (false)
true
, this will display a more compact version of the flipswitch that uses less vertical height by applying the ui-mini
class to the outermost element of the flipswitch widget. This option is also exposed as a data attribute: data-mini="true"
.
offText
"Off"
When the flipswitch widget is based on a checkbox rather than a select
the value of this option is used as the label for the "Off" state.
This option is also exposed as a data attribute: data-off-text="Go"
Initialize the flipswitch with the offText
option specified:
$( ".selector" ).flipswitch({ offText: "Go" });
Get or set the offText
option, after initialization:
// Getter var offText = $( ".selector" ).flipswitch( "option", "offText" ); // Setter $( ".selector" ).flipswitch( "option", "offText", "Go" );
onText
"On"
When the flipswitch widget is based on a checkbox rather than a select
the value of this option is used as the label for the "On" state.
This option is also exposed as a data attribute: data-on-text="Go"
Initialize the flipswitch with the onText
option specified:
$( ".selector" ).flipswitch({ onText: "Stay" });
Get or set the onText
option, after initialization:
// Getter var onText = $( ".selector" ).flipswitch( "option", "onText" ); // Setter $( ".selector" ).flipswitch( "option", "onText", "Stay" );
theme
null, inherited from parent
Possible values: swatch letter (a-z).
This option is also exposed as a data attribute: data-theme="b"
.
Initialize the flipswitch with the theme
option specified:
$( ".selector" ).flipswitch({ theme: "b" });
Get or set the theme
option, after initialization:
// Getter var theme = $( ".selector" ).flipswitch( "option", "theme" ); // Setter $( ".selector" ).flipswitch( "option", "theme", "b" );
wrapperClass
null
div
around the native input
element generated by the framework. This option allows you to specify one or more space-separated class names to be added to the wrapper div
element by the framework. This option is also exposed as a data attribute: data-wrapper-class="custom-class"
.
Initialize the flipswitch with the wrapperClass
option specified:
$( ".selector" ).flipswitch({ wrapperClass: "custom-class" });
Get or set the wrapperClass
option, after initialization:
// Getter var wrapperClass = $( ".selector" ).flipswitch( "option", "wrapperClass" ); // Setter $( ".selector" ).flipswitch( "option", "wrapperClass", "custom-class" );
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
$( ".selector" ).flipswitch( "destroy" );
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
$( ".selector" ).flipswitch( "disable" );
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
$( ".selector" ).flipswitch( "enable" );
option( optionName )Returns: Object
optionName
.- optionNameType: StringThe name of the option to get.
Invoke the method:
var isDisabled = $( ".selector" ).flipswitch( "option", "disabled" );
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
var options = $( ".selector" ).flipswitch( "option" );
option( optionName, value )Returns: jQuery (plugin only)
optionName
.- optionNameType: StringThe name of the option to set.
- valueType: ObjectA value to set for the option.
Invoke the method:
$( ".selector" ).flipswitch( "option", "disabled", true );
option( options )Returns: jQuery (plugin only)
- optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
$( ".selector" ).flipswitch( "option", { disabled: true } );
refresh()Returns: jQuery (plugin only)
If you manipulate a flipswitch 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:
$( ".selector" ).flipswitch( "refresh" );
Events
create( event, ui )Type: flipswitchcreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the flipswitch with the create callback specified:
$( ".selector" ).flipswitch({ create: function( event, ui ) {} });
Bind an event listener to the flipswitchcreate event:
$( ".selector" ).on( "flipswitchcreate", function( event, ui ) {} );
Example:
A basic example of a checkbox in a fieldcontainer
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>flipswitch demo</title> <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="//code.jquery.com/jquery-1.10.2.min.js"></script> <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> </head> <body> <div data-role="header"> <h1>jQuery Mobile Example</h1> </div> <div role="main" class="ui-content"> <div class="ui-field-contain"> <form> <div data-role="fieldcontain"> <label for="checkbox-based-flipswitch-0">Checkbox-based:</label> <input type="checkbox" id="checkbox-based-flipswitch-0" data-role="flipswitch"> </div> </form> </div> </div> </body> </html>