Selectable Widgetversion added: 1.0
Description: Use the mouse to select elements, individually or in a group.
The jQuery UI Selectable plugin allows for elements to be selected by dragging a box (sometimes called a lasso) with the mouse over the elements. Elements can also be selected via click or drag while holding the ctrl/meta key, allowing for multiple (non-contiguous) selections.
Theming
The selectable widget uses the jQuery UI CSS framework to style its look and feel. If selectable specific styling is needed, the following CSS class names can be used for overrides or as keys for the classes
option:
-
ui-selectable
: The selectable element.-
ui-selectee
: One of the selectable elements, as specified through thefilter
option. This element can also receive one of the following classes:ui-selecting
(when the lasso includes this element),ui-selected
(after a successful selection),ui-unselecting
(when the lasso lost this element).
-
-
ui-selectable-helper
: The "lasso" element used to visualize the ongoing selection.
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
appendTo
"body"
Initialize the selectable with the appendTo
option specified:
$( ".selector" ).selectable({ appendTo: "#someElem" });
Get or set the appendTo
option, after initialization:
// Getter var appendTo = $( ".selector" ).selectable( "option", "appendTo" ); // Setter $( ".selector" ).selectable( "option", "appendTo", "#someElem" );
autoRefresh
true
refresh()
method manually.Initialize the selectable with the autoRefresh
option specified:
$( ".selector" ).selectable({ autoRefresh: false });
Get or set the autoRefresh
option, after initialization:
// Getter var autoRefresh = $( ".selector" ).selectable( "option", "autoRefresh" ); // Setter $( ".selector" ).selectable( "option", "autoRefresh", false );
cancel
"input,textarea,button,select,option"
Initialize the selectable with the cancel
option specified:
$( ".selector" ).selectable({ cancel: "a,.cancel" });
Get or set the cancel
option, after initialization:
// Getter var cancel = $( ".selector" ).selectable( "option", "cancel" ); // Setter $( ".selector" ).selectable( "option", "cancel", "a,.cancel" );
classes
{}
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 selectable with the classes
option specified, changing the theming for the ui-selectable
class:
$( ".selector" ).selectable({ classes: { "ui-selectable": "highlight" } });
Get or set a property of the classes
option, after initialization, here reading and changing the theming for the ui-selectable
class:
// Getter var themeClass = $( ".selector" ).selectable( "option", "classes.ui-selectable" ); // Setter $( ".selector" ).selectable( "option", "classes.ui-selectable", "highlight" );
delay
0
Initialize the selectable with the delay
option specified:
$( ".selector" ).selectable({ delay: 150 });
Get or set the delay
option, after initialization:
// Getter var delay = $( ".selector" ).selectable( "option", "delay" ); // Setter $( ".selector" ).selectable( "option", "delay", 150 );
disabled
false
true
.Initialize the selectable with the disabled
option specified:
$( ".selector" ).selectable({ disabled: true });
Get or set the disabled
option, after initialization:
// Getter var disabled = $( ".selector" ).selectable( "option", "disabled" ); // Setter $( ".selector" ).selectable( "option", "disabled", true );
distance
0
Initialize the selectable with the distance
option specified:
$( ".selector" ).selectable({ distance: 30 });
Get or set the distance
option, after initialization:
// Getter var distance = $( ".selector" ).selectable( "option", "distance" ); // Setter $( ".selector" ).selectable( "option", "distance", 30 );
filter
"*"
Initialize the selectable with the filter
option specified:
$( ".selector" ).selectable({ filter: "li" });
Get or set the filter
option, after initialization:
// Getter var filter = $( ".selector" ).selectable( "option", "filter" ); // Setter $( ".selector" ).selectable( "option", "filter", "li" );
tolerance
"touch"
-
"fit"
: Lasso overlaps the item entirely. -
"touch"
: Lasso overlaps the item by any amount.
Initialize the selectable with the tolerance
option specified:
$( ".selector" ).selectable({ tolerance: "fit" });
Get or set the tolerance
option, after initialization:
// Getter var tolerance = $( ".selector" ).selectable( "option", "tolerance" ); // Setter $( ".selector" ).selectable( "option", "tolerance", "fit" );
Methods
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
$( ".selector" ).selectable( "destroy" );
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
$( ".selector" ).selectable( "disable" );
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
$( ".selector" ).selectable( "enable" );
instance()Returns: Object
Retrieves the selectable'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 selectable plugin has loaded.
- This method does not accept any arguments.
Invoke the instance method:
$( ".selector" ).selectable( "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" ).selectable( "option", "disabled" );
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
var options = $( ".selector" ).selectable( "option" );
option( optionName, value )Returns: jQuery (plugin only)
Sets the value of the selectable 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" ).selectable( "option", "disabled", true );
option( options )Returns: jQuery (plugin only)
- optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
$( ".selector" ).selectable( "option", { disabled: true } );
refresh()Returns: jQuery (plugin only)
autoRefresh
option is set to false
.- This method does not accept any arguments.
Invoke the refresh method:
$( ".selector" ).selectable( "refresh" );
widget()Returns: jQuery
jQuery
object containing the selectable element. - This method does not accept any arguments.
Invoke the widget method:
var widget = $( ".selector" ).selectable( "widget" );
Events
create( event, ui )Type: selectablecreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectable with the create callback specified:
$( ".selector" ).selectable({ create: function( event, ui ) {} });
Bind an event listener to the selectablecreate event:
$( ".selector" ).on( "selectablecreate", function( event, ui ) {} );
selected( event, ui )Type: selectableselected
- eventType: Event
- uiType: Object
- selectedType: ElementThe selectable item that has been selected.
-
Initialize the selectable with the selected callback specified:
$( ".selector" ).selectable({ selected: function( event, ui ) {} });
Bind an event listener to the selectableselected event:
$( ".selector" ).on( "selectableselected", function( event, ui ) {} );
selecting( event, ui )Type: selectableselecting
- eventType: Event
- uiType: Object
- selectingType: ElementThe current selectable item being selected.
-
Initialize the selectable with the selecting callback specified:
$( ".selector" ).selectable({ selecting: function( event, ui ) {} });
Bind an event listener to the selectableselecting event:
$( ".selector" ).on( "selectableselecting", function( event, ui ) {} );
start( event, ui )Type: selectablestart
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectable with the start callback specified:
$( ".selector" ).selectable({ start: function( event, ui ) {} });
Bind an event listener to the selectablestart event:
$( ".selector" ).on( "selectablestart", function( event, ui ) {} );
stop( event, ui )Type: selectablestop
Note: The ui
object is empty but included for consistency with other events.
Initialize the selectable with the stop callback specified:
$( ".selector" ).selectable({ stop: function( event, ui ) {} });
Bind an event listener to the selectablestop event:
$( ".selector" ).on( "selectablestop", function( event, ui ) {} );
unselected( event, ui )Type: selectableunselected
- eventType: Event
- uiType: Object
- unselectedType: ElementThe selectable item that has been unselected.
-
Initialize the selectable with the unselected callback specified:
$( ".selector" ).selectable({ unselected: function( event, ui ) {} });
Bind an event listener to the selectableunselected event:
$( ".selector" ).on( "selectableunselected", function( event, ui ) {} );
unselecting( event, ui )Type: selectableunselecting
- eventType: Event
- uiType: Object
- unselectingType: ElementThe current selectable item being unselected.
-
Initialize the selectable with the unselecting callback specified:
$( ".selector" ).selectable({ unselecting: function( event, ui ) {} });
Bind an event listener to the selectableunselecting event:
$( ".selector" ).on( "selectableunselecting", function( event, ui ) {} );
Example:
A simple jQuery UI Selectable.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>selectable demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css"> <style> #selectable .ui-selecting { background: #ccc; } #selectable .ui-selected { background: #999; } </style> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="//code.jquery.com/ui/1.12.0/jquery-ui.js"></script> </head> <body> <ul id="selectable"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul> <script> $( "#selectable" ).selectable(); </script> </body> </html>