$('select[multiple]').multiselect();
Basic 3 Column
Toggle Plugin
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
$('select[multiple]').multiselect({
columns : 3,
search : true,
selectAll: true,
texts : {
placeholder: 'Select States',
search : 'Search States'
}
});
Basic OptGroup 2 Column
Toggle Plugin
C++ / C#
Java
Objective-C
JavaScript
Perl
PHP
Ruby on Rails
Android
iOS (iPhone, iPad and iPod Touch)
HTML
SGML
$('select[multiple]').multiselect({
columns : 2,
search : true,
selectAll: true,
texts : {
placeholder: 'Select Languages',
}
});
Dynamically Populate List
Toggle Plugin
$('select[multiple]').multiselect({
columns : 3,
search : true,
selectAll: true
});
$('select[multiple]').siblings('a.loadoptions')
.click(function( event ){
event.preventDefault();
var options = [{
name : 'Option 1',
value : 1,
checked: false
},{
name : 'Option 2',
value : 2,
checked: true
},{
name : 'Option 3',
value : 3,
checked: false
},{
name : 'Option 4',
value : 4,
checked: true
},{
name : 'Option 5',
value : 5,
checked: false
}];
$(this).siblings('select[multiple]')
.multiselect('loadOptions', options );
});
$('select[multiple]').siblings('a.dpopgetvalues')
.click(function( event ){
event.preventDefault();
alert(
'Selected Values: '
+ $(this).siblings('select[multiple]').val()
);
});
Dynamically Populate List with OptGroups
Toggle Plugin
$('select[multiple]').multiselect({
columns : 3,
search : true,
selectAll : true,
selectGroup: true
});
$('select[multiple]').siblings('a.loadgoptions')
.click(function( event ){
event.preventDefault();
var options = [{
label : 'Group 1',
options: [{
name : 'Option 1',
value : 1,
checked: false
},{
name : 'Option 2',
value : 2,
checked: true
},{
name : 'Option 3',
value : 3,
checked: false
}]
},{
label : 'Group 2',
options: [{
name : 'Option 4',
value : 4,
checked: false
},{
name : 'Option 5',
value : 5,
checked: true
},{
name : 'Option 6',
value : 6,
checked: false
}]
}];
$(this).siblings('select[multiple]')
.multiselect('loadOptions', options );
});
$('select[multiple]').siblings('a.dpopggetvalues')
.click(function( event ){
event.preventDefault();
alert(
'Selected Values: '
+ $(this).siblings('select[multiple]').val()
);
});
Dynamically Add Option to List
Toggle Plugin
$('select[multiple]').multiselect({
columns : 3,
search : true,
selectAll: true
});
$('select[multiple]').siblings('a.addoption')
.click(function( event ){
event.preventDefault();
var optionCnt = $(this).siblings('select[multiple]')
.find('option').length + 1;
$(this).siblings('select[multiple]')
.multiselect('loadOptions', [{
name : 'Option '+ optionCnt,
value : optionCnt,
checked: false
}],
false
);
});
$('select[multiple]').siblings('a.daddoptgetvalues')
.click(function( event ){
event.preventDefault();
alert(
'Selected Values: '
+ $(this).siblings('select[multiple]').val()
);
});
Get selected values from list
Toggle Plugin
NOTE: There is no special way to get the selected values than regular jQuery or JavaScript. With jQuery you simple use $('select[multiple]').val(); to get the selected values.
$('select[multiple]').multiselect();
$('select[multiple]').siblings('a.getvalues')
.click(function( event ){
event.preventDefault();
alert(
'Selected Values: '
+ $(this).siblings('select[multiple]').val()
);
});
Disable/Enable/Reset Select list
Toggle Plugin
$('select[multiple]').multiselect();
$('select[multiple]').siblings('a.tstate')
.click(function( event ){
event.preventDefault();
var newState = $(this).siblings('select[multiple]')
.attr('disabled') ? false : true;
$(this).siblings('select[multiple]')
.multiselect( 'disable', newState );
});
$('select[multiple]').siblings('a.treset')
.click(function( event ){
event.preventDefault();
$(this).siblings('select[multiple]')
.multiselect( 'reset' );
});
$('select[multiple]').siblings('a.tshowhide')
.click(function( event ){
event.preventDefault();
$(this).siblings('.ms-options-wrap')
.toggle();
});
$('select[multiple]').siblings('a.dergetvalues')
.click(function( event ){
event.preventDefault();
alert(
'Selected Values: '
+ $(this).siblings('select[multiple]').val()
);
});
Event when option (de)selected
Toggle Plugin
Option 1
Option 2
Option 3
Option 4
Option 5
$('select[multiple]').multiselect({
onOptionClick: function( element, option ){
var thisOpt = $(option);
alert(
'The Option "'+ thisOpt.val() +'" was '
+ (thisOpt.prop('checked') ? '' : 'de') +'selected'
);
}
});
Options with data attributes
Toggle Plugin
Option 1
Option 2
Option 3
Option 4
Option 5
$('select[multiple]').multiselect({
optionAttributes: ['data-foo']
});
Max Selection of Options
Toggle Plugin
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
Florida
Georgia
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Ohio
Oklahoma
Oregon
Pennsylvania
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Washington
West Virginia
Wisconsin
Wyoming
$('select[multiple]').multiselect({
columns : 3,
search : true,
texts : {
placeholder: 'Select States (max 3)',
search : 'Search States'
},
onOptionClick: function( element, option ) {
var maxSelect = 3;
// too many selected, deselect this option
if( $(element).val().length > maxSelect ) {
if( $(option).is(':checked') ) {
var thisVals = $(element).val();
thisVals.splice(
thisVals.indexOf( $(option).val() ), 1
);
$(element).val( thisVals );
$(option).prop( 'checked', false ).closest('li')
.toggleClass('selected');
}
}
// max select reached, disable non-checked checkboxes
else if( $(element).val().length == maxSelect ) {
$(element).next('.ms-options-wrap')
.find('li:not(.selected)').addClass('disabled')
.find('input[type="checkbox"]')
.attr( 'disabled', 'disabled' );
}
// max select not reached, make sure any disabled
// checkboxes are available
else {
$(element).next('.ms-options-wrap')
.find('li.disabled').removeClass('disabled')
.find('input[type="checkbox"]')
.removeAttr( 'disabled' );
}
}
});