<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<select multiple="" id="all-local-resources-1">
<option value="brownfields">Brownfields and Re-development and Industrial Cleanup</option>
<option value="climate">Climate Change</option>
<option value="conservation">Conservation and Alternative Energy</option>
<option value="drinking">Drinking Water and Wastewater</option>
</select>
<select multiple="" id="all-local-resources-2">
<optgroup label="Group 1">
<option value="brownfields">Brownfields and Re-development and Industrial Cleanup</option>
<option value="climate">Climate Change</option>
</optgroup>
<optgroup label="Group 2">
<option value="conservation">Conservation and Alternative Energy</option>
<option value="drinking">Drinking Water and Wastewater</option>
</optgroup>
</select>
JavaScript
// CREDIT TO CREATOR: https://gist.github.com/mtayseer/5743199
(function($) {
function addUL(parent) {
var $ul = $("<ul/>");
var baseId = "_" + $(parent).attr("id");
$(parent).children("option,optgroup").each(function(index, tag) {
if (tag.tagName.toLowerCase() === "option") {
var $option = $(this);
var id = baseId + index;
var $li = $("<li/>").appendTo($ul);
var $checkbox = $("<input type='checkbox' id='" + id + "'/>").appendTo($li).change(function() {
if ($(this).is(":checked")) {
$option.attr("selected", "selected");
} else {
$option.removeAttr("selected");
}
});
if ($option.is(":selected")) {
$checkbox.attr("checked", "checked");
}
$checkbox.after("<label for='" + id + "'>" + $option.text() + "</label>");
} else {
var optGroupUL = addUL(this);
var $li = $('<li>' + this.label + '</li>').appendTo($ul)
optGroupUL.appendTo($ul);
}
});
return $ul;
}
var methods = {
init: function() {
var ul = addUL(this);
ul.insertAfter(this);
ul.addClass('multiple_selection');
$(this).hide();
}
};
$.fn.multiSelectToCheckboxes = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.multiSelectToCheckboxes');
}
};
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.