Multiselect

This plugin is a drop-in replacement for the standard <select> element with multiple attribute activated.

  • Free (under WTFPL license)
  • Works in an unobtrusive fashion
  • Fully open sourced
  • Keyboard support
  • Provides some callbacks
  • Fully customizable via CSS
  • Depends on jQuery 1.8+
  • Tiny code ~8kb minified

HTML

<html>
  <head>
    <link href="path/to/multiselect.css" media="screen" rel="stylesheet" type="text/css">
  </head>
  <body>
    <select multiple="multiple" id="my-select" name="my-select[]">
      <option value='elem_1'>elem 1</option>
      <option value='elem_2'>elem 2</option>
      <option value='elem_3'>elem 3</option>
      <option value='elem_4'>elem 4</option>
      ...
      <option value='elem_100'>elem 100</option>
    </select>
    <script src="path/to/jquery.multi-select.js" type="text/javascript"></script>
  </body>
</html>

JavaScript

$('#my-select').multiSelect()

Options

Name type default description
afterInit function function(container){} Function to call after the multiSelect initilization.
afterSelect function function(values){} Function to call after one item is selected.
afterDeselect function function(values){} Function to call after one item is deselected.
selectableHeader HTML/Text null Text or HTML to display in the selectable header.
selectionHeader HTML/Text null Text or HTML to display in the selection header.
selectableFooter HTML/Text null Text or HTML to display in the selectable footer.
selectionFooter HTML/Text null Text or HTML to display in the selection footer.
disabledClass String 'disabled' CSS class for disabled items.
selectableOptgroup Boolean false Click on optgroup will select all nested options when set to true.

Methods


.multiSelect(options)

Activates your content as a multiselect. Accepts an optional options object

$('#your-select').multiSelect({});

Note: You must init the multiple select with $('#your-select').multiSelect() before calling one of the following methods.

.multiSelect('select', String|Array)

Select the item with the value given in parameter. The value can be either a string ('elem_1') matching the value of the option oran Array of values (['elem_1', 'elem_42']).

$('#your-select').multiSelect('select', String|Array);

.multiSelect('deselect', String|Array)

Deselect the item with the value given in parameter. The value can be either a string ('elem_1') matching the value of the option oran Array of values (['elem_1', 'elem_42']).

$('#your-select').multiSelect('deselect', String|Array);

.multiSelect('select_all')

Select all elements.

$('#your-select').multiSelect('select_all');

.multiSelect('deselect_all')

Deselect all items previously selected.

$('#your-select').multiSelect('select_all');

.multiSelect('refresh')

Refresh current multiselect.

$('#your-select').multiSelect('refresh');

Keyboard

key function
[  ↓  ]  Down arrow Select next item in the focused list
[  ↑  ]  Up arrow Select previous item in the focused list
[ — ]  Space Add/remove item depending on which list is focused
[ ← ]  Left arrow Focus on the next list
[ → ]  Right arrow Focus on the previous list

Pre-selected options

<select id='pre-selected-options' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#pre-selected-options').multiSelect();

Callbacks

<select id='Callbacks' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#callbacks').multiSelect({
  afterSelect: function(values){
    alert("Select value: "+values);
  },
  afterDeselect: function(values){
    alert("Deselect value: "+values);
  }
});

Public methods

<a href='#' id='select-all'>select all</a>
<a href='#' id='deselect-all'>deselect all</a>
<a href='#' id='select-100'>select 100 elems</a>
<a href='#' id='deselect-100'>deselect 100 elems</a>
<select id='public-methods' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_1000'>elem 100</option>
</select>
$('#public-methods').multiSelect();
$('select-all').click(function(){
  $('#public-methods').multiSelect('select_all');
  return false;
});
$('deselect-all').click(function(){
  $('#public-methods').multiSelect('deselect_all');
  return false;
});
$('select-100').click(function(){
  $('#public-methods').multiSelect('select', ['elem_0', 'elem_1' ..., 'elem_99']);
  return false;
});
$('deselect-100').click(function(){
  $('#public-methods').multiSelect('deselect', ['elem_0', 'elem_1' ..., 'elem_99']);
  return false;
});
select all · deselect all · select 100 elems · deselect 100 elems


In this example there are 1000 elements


Optgroup

<select id='optgroup' multiple='multiple'>
  <optgroup label='Friends'>
    <option value='1'>Yoda</option>
    <option value='2'>Obiwan</option>
  </optgroup>
  <optgroup label='Enemies'>
    <option value='3'>Palpatine</option>
    <option value='4'>Darth Vader</option>
  </optgroup>
</select>
$('#optgroup').multiSelect({ selectableOptgroup: true });

Disabled attribute

<select id='disabled-attribute' disabled='disabled' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#disabled-attribute').multiSelect();
Note: You can also deactivate option one by one by adding disabled attribute to each option you want to disable
<option value='fuu' disabled='disabled'>bar</option>

Custom headers and footers

<select id='custom-headers' multiple='multiple'>
  <option value='elem_1'>elem 1</option>
  <option value='elem_2'>elem 2</option>
  <option value='elem_3'>elem 3</option>
  <option value='elem_4'>elem 4</option>
  ...
  <option value='elem_100'>elem 100</option>
</select>
$('#custom-headers').multiSelect({
  selectableHeader: "<div class='custom-header'>Selectable items</div>",
  selectionHeader: "<div class='custom-header'>Selection items</div>",
  selectableFooter: "<div class='custom-header'>Selectable footer</div>",
  selectionFooter: "<div class='custom-header'>Selection footer</div>"
});

Searchable

Note: This feature is not built-in but depends on an other plugin. I personnally use the excellent quicksearch library, but you can use whatever library you like.

<select id='searchable' multiple='multiple'>
  <option value='fr'>France</option>
  <option value='uk'>United Kingdom</option>
  <option value='us'>United States</option>
  <option value='ch'>China</option>
</select>
<script src="path/to/jquery.multi-select.js" type="text/javascript"></script>
<script src="path/to/jquery.quicksearch.js" type="text/javascript"></script>
$('#searchable').multiSelect({
  selectableHeader: "<input type='text' id='search' autocomplete='off' placeholder='try \"elem 2\"'>"
});

$('#search').quicksearch($('.ms-elem-selectable', '#ms-searchable' )).on('keydown', function(e){
  if (e.keyCode == 40){
    $(this).trigger('focusout');
    $('#searchable').focus();
    return false;
  }
});

How to contribute?