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
:
1
2
3
4
5
6
|
|
onText
and offText
options.
Select-based flipswitch
You can also create a flipswitch based on a select
element:
1
2
3
4
5
6
7
8
9
|
|
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:
1
2
3
4
5
6
|
|
Depending on your choice of labels, you may also have to provide custom CSS to override the default width for the flipswitch:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|
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.
1
2
3
4
5
|
|
Options
corners
true
This option is also exposed as a data attribute: data-corners="false"
.
Initialize the flipswitch with the corners
option specified:
1
2
3
|
|
Get or set the corners
option, after initialization:
1
2
3
4
5
|
|
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:
1
2
3
|
|
Get or set the defaults
option, after initialization:
1
2
3
4
5
|
|
disabled
false
true
.
This option is also exposed as a data attribute: data-disabled="true"
.
Initialize the flipswitch with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
enhanced
false
This option is also exposed as a data attribute: data-enhanced="true"
.
Initialize the flipswitch with the enhanced
option specified:
1
2
3
|
|
Get or set the enhanced
option, after initialization:
1
2
3
4
5
|
|
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:
1
2
3
|
|
Get or set the offText
option, after initialization:
1
2
3
4
5
|
|
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:
1
2
3
|
|
Get or set the onText
option, after initialization:
1
2
3
4
5
|
|
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:
1
2
3
|
|
Get or set the theme
option, after initialization:
1
2
3
4
5
|
|
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:
1
2
3
|
|
Get or set the wrapperClass
option, after initialization:
1
2
3
4
5
|
|
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
option( optionName )Returns: Object
optionName
.-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
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:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
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:
1
|
|
1
|
|
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:
1
2
3
|
|
Bind an event listener to the flipswitchcreate event:
1
|
|
Example:
A basic example of a checkbox in a fieldcontainer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
|