Checkboxradio Widgetversion added: 1.12
Description: Converts inputs of type radio and checkbox to themeable buttons.
Native HTML input elements are impossible to style consistently. This widget allows working around that limitation by positining the associated label on top of the hidden input, and emulating the checkbox or radio element itself using an (optional) icon. The original input still receives focus and all events, the label merely provides a themeable button on top.
Theming
The checkboxradio widget uses the jQuery UI CSS framework to style its look and feel. If checkboxradio specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes
option:
-
ui-checkboxradio
: The input of type radio or checkbox. Will be hidden, with its associated label positioned on top.-
ui-checkboxradio-label
: The label associated with the input. If the input is checked, this will also get theui-checkboxradio-checked
class. If the input is of type radio, this will also get theui-checkboxradio-radio-label
class. -
ui-checkboxradio-icon
: If theicon
option is enabled, the generated icon has this class. -
ui-checkboxradio-icon-space
: If theicon
option is enabled, an extra element with this class as added between the text label and the icon.
-
Dependencies
Additional Notes:
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
Options
classes
{ "ui-checkboxradio-label": "ui-corner-all", "ui-checkboxradio-icon": "ui-corner-all" }
Specify additional classes to add to the widget's elements. Any of classes specified in the Theming section can be used as keys to override their value. To learn more about this option, check out the learn article about the classes
option.
Initialize the checkboxradio with the classes
option specified, changing the theming for the ui-checkboxradio
class:
$( ".selector" ).checkboxradio({ classes: { "ui-checkboxradio": "highlight" } });
Get or set a property of the classes
option, after initialization, here reading and changing the theming for the ui-checkboxradio
class:
// Getter var themeClass = $( ".selector" ).checkboxradio( "option", "classes.ui-checkboxradio" ); // Setter $( ".selector" ).checkboxradio( "option", "classes.ui-checkboxradio", "highlight" );
disabled
false
true
.Initialize the checkboxradio with the disabled
option specified:
$( ".selector" ).checkboxradio({ disabled: true });
Get or set the disabled
option, after initialization:
// Getter var disabled = $( ".selector" ).checkboxradio( "option", "disabled" ); // Setter $( ".selector" ).checkboxradio( "option", "disabled", true );
icon
true
Initialize the checkboxradio with the icon
option specified:
$( ".selector" ).checkboxradio({ icon: false });
Get or set the icon
option, after initialization:
// Getter var icon = $( ".selector" ).checkboxradio( "option", "icon" ); // Setter $( ".selector" ).checkboxradio( "option", "icon", false );
label
null
null
), the HTML content of the associated <label>
element is used.Initialize the checkboxradio with the label
option specified:
$( ".selector" ).checkboxradio({ label: "custom label" });
Get or set the label
option, after initialization:
// Getter var label = $( ".selector" ).checkboxradio( "option", "label" ); // Setter $( ".selector" ).checkboxradio( "option", "label", "custom label" );
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
$( ".selector" ).checkboxradio( "destroy" );
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
$( ".selector" ).checkboxradio( "disable" );
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
$( ".selector" ).checkboxradio( "enable" );
instance()Returns: Object
Retrieves the checkboxradio's instance object. If the element does not have an associated instance, undefined
is returned.
Unlike other widget methods, instance()
is safe to call on any element after the checkboxradio plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
$( ".selector" ).checkboxradio( "instance" );
option( optionName )Returns: Object
Gets the value currently associated with the specified optionName
.
Note: For options that have objects as their value, you can get the value of a specific key by using dot notation. For example, "foo.bar"
would get the value of the bar
property on the foo
option.
- optionNameType: StringThe name of the option to get.
Invoke the method:
var isDisabled = $( ".selector" ).checkboxradio( "option", "disabled" );
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
var options = $( ".selector" ).checkboxradio( "option" );
option( optionName, value )Returns: jQuery (plugin only)
Sets the value of the checkboxradio option associated with the specified optionName
.
Note: For options that have objects as their value, you can set the value of just one property by using dot notation for optionName
. For example, "foo.bar"
would update only the bar
property of the foo
option.
- optionNameType: StringThe name of the option to set.
- valueType: ObjectA value to set for the option.
Invoke the method:
$( ".selector" ).checkboxradio( "option", "disabled", true );
option( options )Returns: jQuery (plugin only)
- optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
$( ".selector" ).checkboxradio( "option", { disabled: true } );
refresh()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the refresh method:
$( ".selector" ).checkboxradio( "refresh" );
widget()Returns: jQuery
jQuery
object containing the checkboxradio. - This method does not accept any arguments.
Invoke the widget method:
var widget = $( ".selector" ).checkboxradio( "widget" );
Events
create( event, ui )Type: checkboxradiocreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the checkboxradio with the create callback specified:
$( ".selector" ).checkboxradio({ create: function( event, ui ) {} });
Bind an event listener to the checkboxradiocreate event:
$( ".selector" ).on( "checkboxradiocreate", function( event, ui ) {} );
Example:
A simple jQuery UI checkboxradio
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>checkboxradio demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <fieldset> <legend>Select a Location: </legend> <label for="radio-1">New York</label> <input type="radio" name="radio-1" id="radio-1"> <label for="radio-2">Paris</label> <input type="radio" name="radio-1" id="radio-2"> <label for="radio-3">London</label> <input type="radio" name="radio-1" id="radio-3"> </fieldset> <script> $( "input[type='radio']" ).checkboxradio(); </script> </body> </html>