jQuery UI Selectable

To select a DOM element individually or in a group, by dragging a box with the mouse over the element, the jQuery UI selectable() method is used. The ctrl button can be used to select multiple elements.

Syntax:

The selectable () method can be used in two forms:

  • $(selector, context).selectable (options) Method
  • $(selector, context).selectable (“action”, params) Method

First Method:

To specify that an HTML element contains selectable items, the selectable (options) method. To define the behavior of the elements involved when selecting, the “options” parameter is used as an object.

For selecting multiple options at a time using a JavaScript object, the options are separated using commas.

Syntax:

$(selector, context).selectable({option1: value1, option2: value2..... });

The popular options that can be used with this method are listed below:

Option Uses
appendTo To define the element the selection helper (the lasso) should be appended to. The default value is body.
autoRefresh To compute the position and size of each selectable item at the beginning of a select operation, when set option to true. The default value is true.
cancel To forbid selecting if the selection of elements is started. The default value is input, textArea, button, select, option.
delay To indicate when the selecting should start, by setting the time in milliseconds, thus preventing unwanted selections. The default value is 0.
disabled To disable the selection mechanism, when set to true. The selection of the elements is not possible until the mechanism is set to enable. The default value is false.
distance To specify the distance (in pixels) the mouse must move to consider the selection in progress. It is useful in preventing simple clicks from being interpreted as a group selection and other similar actions. The default value is 0.
filter To indicate which elements can be part of the selection. The default value is *. It is a selector.
tolerance To define the mode to be used for testing whether the selection helper (the lasso) should select an item. The default value is touch.

Example 1:

 
<!doctype html>  
<html lang="en">  
   <head>  
      <meta charset="utf-8">  
      <title>jQuery UI selectable-1</title>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
      <style>  
         #select-1 .ui-selecting { background: yellow ; }  
         #select-1 .ui-selected { background: crimson; color: black; }  
         #select-1 { list-style-type: none; margin: 0;   
            padding: 0; width:200px; }  
         #select-1 li { margin: 5px; padding: 0.4em;   
            font-size: 18px; height: 20px; }  
         .ui-widget-content {  
            background: gray;  
            border: 2px solid turquoise;  
            color: white;  
         }  
      </style>  
      <script>  
         $(function() {  
            $( "#select-1" ).selectable();  
         });  
      </script>  
   </head>  
   <body>  
      <ol id="select-1">  
         <li class="ui-widget-content">First</li>  
         <li class="ui-widget-content">Second</li>  
         <li class="ui-widget-content">Third</li>  
         <li class="ui-widget-content">Fourth</li>  
         <li class="ui-widget-content">Fifth</li>  
         <li class="ui-widget-content">Sixth</li>  
         <li class="ui-widget-content">Seventh</li> 
         <li class="ui-widget-content">Eighth</li> 
      </ol>   
   </body>  
</html>

Output 1:

Output 2:

Output 3:

Explanation:

In the above example, we are displaying the selectable functionality. Here, we are passing no parameter to the selectable() method.

Example 2:

<!doctype html>  
<html lang="en">  
   <head>  
      <meta charset="utf-8">  
      <title>jQuery UI Selectable</title>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
 
      <style>  
         #select-1 .ui-selecting,#select-2 .ui-selecting {   
            background: gray ; }  
         #select-1 .ui-selected,#select-2 .ui-selected {   
            background: crimson; color: white; }  
         #select-1,#select-2 { list-style-type: none; margin: 0;   
            padding: 0; width: 200px; }  
         #select-1 li,#select-2 li { margin: 5px; padding: 0.4em;   
            font-size: 18px; height: 20px; }  
         .ui-widget-content {  
            background: turquoise;  
            border: 2px solid black;  
            color: black;  
         }  
      </style>  
      <script>  
         $(function() {  
            $( "#select-1" ).selectable({  
               delay : 500  
            });  
            $( "#select-2" ).selectable({  
               distance : 150  
            });  
         });  
      </script>  
   </head>  
   <body>  
      <h3>Requires delay of 500ms</h3>  
      <ol id="select-1">  
         <li class="ui-widget-content">First</li>  
         <li class="ui-widget-content">Second</li>  
         <li class="ui-widget-content">Third</li>  
         <li class="ui-widget-content">Fourth</li>  
      </ol>  
      <h3>Requires mouse to move a distance of 150px</h3>  
      <ol  id="select-2">  
         <li class="ui-widget-content">Fifth</li>  
         <li class="ui-widget-content">Sixth</li>  
         <li class="ui-widget-content">Seventh</li> 
         <li class="ui-widget-content">Eighth</li> 
      </ol>  
   </body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the use of the two options delay and distance. Here, the first list allows selection of the items after a delay of 500ms, while the second list allows the selection of the items after the mouse cursor moves a distance of 150 px.

Second Method:

To act on selectable elements, the selectable (“action”, params) method is used. It can be used to prevent selectable functionality. The first argument of this method is “action” which is a string. To stop the selection, the value of action is passed as “disable”.

Syntax:

$(selector, context).selectable ("action", params)

The popular actions that can be used with this method are listed below:

Action Uses
destroy To destroy the functionality of an element completely, so that the elements return to their pre-init state.
disable To disable the selectable functionality of an element. No arguments is accepted by this method.
enable To enable the selectable functionality of an element. No arguments is accepted by this method.
option( optionName, value ) To retrieve the value currently associated with the specified optionName.
option() To retrieve an object containing key/value pairs representing the current selectable options hash.
option( optionName, value ) To set the value of the selectable option associated with the specified optionName. The name of the option to be set is specified by the argument optionName and the value to be set for the option is specified by the argument value.
option( options ) To set one or more options for the selectable. A map of option-value pairs to be set is specified by the argument options.
refresh To refresh the size and position of the selectable elements, especially when the autoRefresh option is disabled (set to false). No arguments is accepted by this method.
widget To retrieve a jQuery object containing the selectable element. No arguments is accepted by this method.

Example 3:

<!doctype html>  
<html lang="en">  
   <head>  
      <meta charset="utf-8">  
      <title>jQuery UI Selectable</title>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
      <style>  
         #select-1 .ui-selecting,#select-2 .ui-selecting {   
            background: gray ; }  
         #select-1 .ui-selected,#select-2 .ui-selected {   
            background: crimson; color: white; } 
         #select-1,#select-2 {   
            list-style-type: none; margin: 0; padding: 0; width: 200px; }  
         #select-1 li,#select-2 li {   
            margin: 5px; padding: 0.4em; font-size: 18px; height: 20px; }  
         .ui-widget-content {  
            background: turquoise;  
            border: 2px solid black;  
            color: black;    
         }  
      </style>  
      <script>  
         $(function() {  
            $( "#select-1" ).selectable();  
            $( "#select-1" ).selectable('disable');  
            $( "#select-2" ).selectable();  
            $( "#select-2" ).selectable( "option", "distance", 1 );   
         });  
      </script>  
   </head>  
   <body>  
      <h3>Disabled</h3>  
      <ol id="select-1">  
         <li class="ui-widget-content">First</li>  
         <li class="ui-widget-content">Second</li>  
         <li class="ui-widget-content">Third</li>  
         <li class="ui-widget-content">Fourth</li> 
      </ol>  
      <h3>Can be Selected</h3>  
      <ol  id="select-2">  
         <li class="ui-widget-content">Fifth</li>  
         <li class="ui-widget-content">Sixth</li>  
         <li class="ui-widget-content">Seventh</li> 
         <li class="ui-widget-content">Eighth</li> 
      </ol>  
   </body>  
</html>

Output 1:

Output 2:

Output 3:

Explanation:

In the above example, we are displaying the use of the disable() and option( optionName, value ) methods. Here, the first list is disabled, and thus selection of items is not allowed in the first list, however, the selection is allowed in the second list of items.

Example 4:

<!doctype html>  
<html lang="en">  
   <head>  
      <meta charset="utf-8">  
      <title>jQuery UI selectable-7</title>  
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
      <style>  
         #select-1 .ui-selecting { background: gray ; }  
         #select-1 .ui-selected { background: crimson; color: white; }  
         #select-1 { list-style-type: none; margin: 0;   
            padding: 0; width: 200px; }  
         #select-1 li { margin: 5px; padding: 0.4em;   
            font-size: 18px; height: 15px; }  
         .ui-widget-content {  
            background: turquoise;  
            border: 2px solid black;  
            color: black;  
         }  
         .resultarea {  
            background: brown;  
            border: 2px solid crimson;   
            color: white;  
            font-size:20px;  
         }  
      </style>  
      <script>  
         $(function() {  
            $( "#select-1" ).selectable({  
               selected: function() {  
                  var result = $( "#result" ).empty();  
                  $( ".ui-selected", this ).each(function() {  
                     var index = $( "#select-1 li" ).index( this );  
                     result.append( " #" + ( index + 1 ) );  
                  });  
               }  
            });  
         });  
      </script>  
   </head>  
   <body>  
      <h3>List of Events</h3>  
      <ol id="select-1">  
         <li class="ui-widget-content">First</li>  
         <li class="ui-widget-content">Second</li>  
         <li class="ui-widget-content">Third</li>  
         <li class="ui-widget-content">Fourth</li>  
         <li class="ui-widget-content">Fifth</li>  
         <li class="ui-widget-content">Sixth</li>  
         <li class="ui-widget-content">Seventh</li> 
         <li class="ui-widget-content">Eighth</li> 
      </ol>  
      <span class="resultarea">Selected Events::</span>  
      <span id=result class="resultarea"></span>  
   </body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the way to use the event method with selectable functionality, by displaying the use and behavior of the event “selected” with selectable functionality. Here, the id (index + 1) of all the selected events are listed in the result area of the page which is otherwise vacant.

Please Share