jQuery multiselect drop down menu
Update (2017): The following two libraries have now become the most common drop-down libraries used with Javascript. While they are jQuery-native, they have been customized to work with everything from AngularJS 1.x to having custom CSS for Bootstrap. (Chosen JS, the original answer here, seems to have dropped to #3 in popularity.)
- Select2: https://select2.github.io/
- Selectize: http://selectize.github.io/selectize.js/
Obligatory screenshots below.
Select2:
Selectize:
Original answer (2012): I think that the Chosen library might also be useful. Its available in jQuery, Prototype and MooTools versions.
Attached is a screenshot of how the multi-select functionality looks in Chosen.
I was also looking for a simple multi select for my company. I wanted something simple, highly customizable and with no big dependencies others than jQuery.
I didn't found one fitting my needs so I decided to code my own.
I use it in production.
Here's some demos and documentation: loudev.com
If you want to contribute, check the github repository
Download jquery.multiselect
-
Include the jquery.multiselect.js and jquery.multiselect.css files
<script src="jquery-ui-multiselect-widget-master/src/jquery.multiselect.js" type="text/javascript"></script> <link rel="stylesheet" href="jquery-ui-multiselect-widget-master/jquery.multiselect.css" />
Populate your select input
-
Add the multiselect
$('#' + Field).multiselect({ checkAllText: "Your text for CheckAll", uncheckAllText: "Your text for UncheckCheckAll", noneSelectedText: "Your text for NoOptionHasBeenSelected", selectedText: "You selected # of #" //The multiselect knows to display the second # as the total });
-
You may change the style
ui-multiselect { //The button background:#fff !important; //background-color wouldn't work here text-align: right !important; } ui-multiselect-header { //The CheckAll/ UncheckAll line) background: lightgray !important; text-align: right !important; } ui-multiselect-menu { //The options text-align: right !important; }
-
If you want to repopulate the select, you must refresh it:
$('#' + Field).multiselect('refresh');
-
To get the selected values (comma separated):
var SelectedOptions = $('#' + Field).multiselect("getChecked").map(function () { return this.value; }).get();
-
To clear all selected values:
$('#' + Field).multiselect("uncheckAll");