(function($) {
$(function (){ //When an option is selected in bootstrap-select it should be added to Selected optgroup
$('body').on("changed.bs.select", ".bootstrap-select", function(e, clickedIndex, isSelected, previousValue){
var element = $(this);
var selectElement = element.find("select");
var optgroup = selectElement.find("optgroup.selected");
if (optgroup.length == 0) {
optgroup = $("<optgroup/>", {class: "selected"});
selectElement.prepend(optgroup);
}
//If selected adds options to optgroup else removes from optgroup
var clickedElement = selectElement.find("option").filter(":eq(" + clickedIndex + ")");
if (isSelected) {
clickedElement.detach().appendTo(optgroup);
} else {
clickedElement.detach().insertAfter(optgroup);
}
//When selectall option like All is selected rest options should be deselected and when any option is selected selectall should be removed from optgroup
if (clickedElement.hasClass("selectall") && isSelected) {
optgroup.find("option").not(".selectall").removeAttr("selected").detach().insertAfter(optgroup);
} else if (!clickedElement.hasClass("selectall") && isSelected) {
optgroup.find("option.selectall").removeAttr("selected").detach().insertAfter(optgroup);
}
//When selectall option exist it should be selected if nothing is selected
if (optgroup.find("option").length == 0) {
if (selectElement.find("option.selectall").length != 0)
selectElement.find("option.selectall").attr("selected", "selected").detach().appendTo(optgroup);
else
optgroup.remove();
}
selectElement.selectpicker("refresh");
});
});
}(jQuery))
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.