<!DOCTYPE html> <html> <head> <title>Bootstrap Multiselect</title> <meta name="robots" content="noindex, nofollow" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta name="copyright" content="David Stutz" /> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"> <link rel="stylesheet" href="css/bootstrap-responsive.min.css" type="text/css"> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/bootstrap-multiselect.js"></script> </head> <body> <script type="text/javascript"> $(document).ready(function() { $('#example1').multiselect(); $('#example2').multiselect(); $('#example3').multiselect({ buttonClass: 'btn btn-link' }); $('#example4').multiselect({ buttonClass: 'btn btn-small' }); $('#example5').multiselect({ buttonClass: 'btn btn-primary disabled' }); $('#example6').multiselect(); $('.example7').multiselect({ buttonContainer: '<span class="dropdown" />', }); $('#example9').multiselect({ onChange:function(element, checked){ alert('Change event invoked!'); console.log(element); } }); for (var i = 1; i <= 100; i++) { $('#example11').append('<option value="' + i + '">Options ' + i + '</option>'); } $('#example11').multiselect({ maxHeight: 200, }) }); </script> <div class="container"> <div class="hero-unit"> <h1>Bootstrap Multiselect</h1> <p> Bootstrap Multiselect is a JQuery based plugin to provide an intuitive user interface for using select inputs with the multiple attribute present. Instead of a select a bootstrap button will be shown as dropdown menu containing the single options as checkboxes. </p> </div> <p class="alert alert-info"> <b>Note:</b> The option names may have changed due to the latest updates. </p> <div class="page-header"> <h1>Examples</h1> </div> <p> The best way learning from the examples is using firebug, chrome developer tools or similar tools for your browser. Examine the generated markup and used javascript code. </p> <table class="table table-striped"> <tr> <td> <select id="example1"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> Normal select. The plugin will add the <code>multiple="multiple"</code> attribute automatically. The first option is selected automatically by the browser when ommitting the <code>multiple="multiple"</code> attribute. </td> </tr> <tr> <td> <select id="example2" multiple="multiple"> <option value="cheese" selected>Cheese</option> <option value="tomatoes" selected>Tomatoes</option> <option value="mozarella" selected>Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> Select with preselected options: <code><option value="cheese" selected>Cheese</option></code> </td> </tr> <tr> <td> <select id="example3" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> As link using <code>buttonClass: 'btn btn-link'</code>. </td> </tr> <tr> <td> <select id="example4" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> Small button using <code>buttonClass: 'btn btn-small'</code>. </td> </tr> <tr> <td> <select id="example5" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> Disabled using <code>buttonClass: 'btn btn-primary disabled'</code>. </td> </tr> <tr> <td> <div class="input-prepend input-append btn-toolbar"> <span class="add-on"><b class="icon-list-alt"></b></span> <select id="example6" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <button class="btn btn-danger">Cancel</button> <button class="btn btn-success">Save</button> </div> </td> <td> Multiple select within a group with add-ons and default container for the plugin: <code>buttonContainer: '<div class="btn-group" />'</code>. </td> </tr> <tr> <td> <div class="input-prepend input-append"> <span class="add-on"><b class="icon-list-alt"></b></span> <select class="example7" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <select class="example7" multiple="multiple"> <option value="large">Extra Cheese</option> <option value="extra">Extra Large</option> </select> <button class="btn btn-primary">Save</button> </div> </td> <td> Multiple selects within a group using <code>buttonContainer: '<span class="dropdown" />'</code>. </td> </tr> <tr> <td> <select id="example9" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> </td> <td> Using the <code>onChange</code> option you can add an event handler to the change event. The event handler gets the selected option as first parameter and a variable saying whether the option is checked or not as second one. </td> </tr> <tr> <td> <select id="example11" multiple="multiple"> </select> </td> <td> For long option lists the <code>maxheight</code> option can be set. </td> </tr> </table> <div class="page-header"> <h1>Methods</h1> </div> <script type="text/javascript"> $(document).ready(function() { $('#example8').multiselect({ buttonContainer: '<span />', }); $('#example8-destroy').on('click', function() { $('#example8').multiselect('destroy'); }); $('#example10').multiselect({ buttonContainer: '<span />', }); $('#example10-select').on('click', function() { $('option[value="tomatoes"]', $('#example10')).prop('selected', 'selected'); $('option[value="mushrooms"]', $('#example10')).prop('selected', 'selected'); $('option[value="onions"]', $('#example10')).prop('selected', 'selected'); alert('Selected Tomatoes, Mushrooms and Onions.'); }); $('#example10-deselect').on('click', function() { $('option', $('#example10')).each(function(element) { $(this).removeAttr('selected'); }); }); $('#example10-refresh').on('click', function() { $('#example10').multiselect('refresh'); }); }); </script> <table class="table table-striped"> <tbody> <tr> <td><code>.multiselect('destroy')</code></td> <td> <div class="btn-group"> <select id="example8" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <button id="example8-destroy" class="btn btn-danger">Destroy/Unbind</button> </div> </td> <td> This method is used to destroy the plugin on the given element - meaning unbinding the plugin. </td> </tr> <tr> <td><code>.multiselect('refresh')</code></td> <td> <div class="btn-group"> <select id="example10" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <button id="example10-select" class="btn">Select some options</button> <button id="example10-deselect" class="btn">Deselect all</button> <button id="example10-refresh" class="btn btn-primary">Refresh</button> </div> </td> <td> This method is used to refresh the checked checkboxes based on the currently selected options within the select. Click 'Select some options' so select some of the options (meaning added the <code>selected</code> attribute to some of the options). Then click refresh. The plugin will update the checkboxes accordingly. </td> </tr> </tbody> </table> <div class="page-header"> <h1>Code</h1> </div> <p> Basic markup used in the above examples: </p> <pre> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"> <link rel="stylesheet" href="css/bootstrap-responsive.min.css" type="text/css"> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/bootstrap.min.js"></script> <script type="text/javascript" src="js/bootstrap-multiselect.js"></script> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ buttonClass: 'btn', buttonWidth: 'auto', buttonText: function(options) { if (options.length == 0) { return 'None selected <b class="caret"></b>'; } else if (options.length > 3) { return options.length + ' selected <b class="caret"></b>'; } else { var selected = ''; options.each(function() { selected += $(this).text() + ', '; }); return selected.substr(0, selected.length -2) + ' <b class="caret"></b>'; } } }); }); </script> <select class="multiselect" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <div class="input-prepend input-append"> <span class="add-on"><b class="icon-list-alt"></b></span> <select class="multiselect" multiple="multiple"> <option value="cheese">Cheese</option> <option value="tomatoes">Tomatoes</option> <option value="mozarella">Mozzarella</option> <option value="mushrooms">Mushrooms</option> <option value="pepperoni">Pepperoni</option> <option value="onions">Onions</option> </select> <button class="btn btn-danger">Cancel</button> <button class="btn btn-success">Save</button> </div> </pre> <div class="page-header"> <h1>Options</h1> </div> <table class="table table-striped"> <thead> <tr> <th>Option</th> <th width="30%">Explanation</th> <th>Usage</th> </tr> </thead> <tbody> <tr> <td><code>buttonText</code></td> <td>A function returning the string displayed if options are selected. All currently selected options are passed as argument. In addition HTML can be added to the button, for example the caret icon seen in the examples.</td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ buttonText: function(options) { if (options.length == 0) { return 'None selected <b class="caret"></b>'; } else if (options.length > 3) { return options.length + ' selected <b class="caret"></b>'; } else { var selected = ''; options.each(function() { selected += $(this).text() + ', '; }); return selected.substr(0, selected.length -2) + ' <b class="caret"></b>'; } } }); }); </script> </pre> </td> </tr> <tr> <td><code>buttonClass</code></td> <td>The width of the dropdown button. Default: <code>btn</code>.</td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ buttonClass: 'btn-primary btn-large', }); }); </script> </pre> </td> </tr> <tr> <td><code>buttonWidth</code></td> <td> The width of the dropdown button. Default: <code>auto</code>. Allowed formats: <ul> <li><code>100px</code></li> <li><code>50%</code></li> <li><code>auto</code></li> </ul> </td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ buttonWidth: '300px', }); }); </script> </pre> </td> </tr> <tr> <td><code>buttonContainer</code></td> <td> The used container holding both the dropdown button and the dropdown menu. Default: <code><div class="btn-group" /></code>. </td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ buttonContainer: '<span class="dropdown" />', }); }); </script> </pre> </td> </tr> <tr> <td><code>onChange</code></td> <td> This event handler is triggered when the selected options are changed. </td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ onChange: function(element, checked) { alert('Change event invoked!'); }, }); }); </script> </pre> </td> </tr> <tr> <td><code>maxHeight</code></td> <td> Used for a long list of options this option defines the maximum height of the dropdown menu. If the size is exceeded a scrollbar will appear. </td> <td> <pre> <script type="text/javascript"> $(document).ready(function() { $('.multiselect').multiselect({ maxHeight: 400, }); }); </script> </pre> </td> </tr> </tbody> </table> <hr> <div> © 2012 <a href="http://davidstutz.de">David Stutz</a> - <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License v2.0</a> </div> </div> </body> </html>