The client has given me a design which has a Select Option menu containing a checkbox together with the item name as individual items in the list. Is there anyway possible to add a checkbox inside a Select Option menu?
NB: Developer needs to add his own id to make the menu effective, I only need the HTML CSS code if it is possible.
This question is related to
javascript
html
css
html-select
Try multiple-select, especially multiple-items. Looks to be much clean and managed solution, with tons of examples. You can also view the source.
<div>
<div class="form-group row">
<label class="col-sm-2">
Basic Select
</label>
<div class="col-sm-10">
<select multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">
Group Select
</label>
<div class="col-sm-10">
<select multiple="multiple">
<optgroup label="Group 1">
<option value="1">1</option>
<option value="2">2</option>
</optgroup>
<optgroup label="Group 2">
<option value="6">6</option>
<option value="7">7</option>
</optgroup>
<optgroup label="Group 3">
<option value="11">11</option>
<option value="12">12</option>
</optgroup>
</select>
</div>
</div>
</div>
<script>
$(function() {
$('select').multipleSelect({
multiple: true,
multipleWidth: 60
})
})
</script>
Alternate Vanilla JS version with click outside to hide checkboxes:
let expanded = false;
const multiSelect = document.querySelector('.multiselect');
multiSelect.addEventListener('click', function(e) {
const checkboxes = document.getElementById("checkboxes");
if (!expanded) {
checkboxes.style.display = "block";
expanded = true;
} else {
checkboxes.style.display = "none";
expanded = false;
}
e.stopPropagation();
}, true)
document.addEventListener('click', function(e){
if (expanded) {
checkboxes.style.display = "none";
expanded = false;
}
}, false)
I'm using addEventListener instead of onClick in order to take advantage of the capture/bubbling phase options along with stopPropagation(). You can read more about the capture/bubbling here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
The rest of the code matches vitfo's original answer (but no need for onclick() in the html). A couple of people have requested this functionality sans jQuery.
Here's codepen example https://codepen.io/davidysoards/pen/QXYYYa?editors=1010
You might be loading multiselect.js file before the option list updated with AJAX call so while execution of multiselect.js file there is empty option list is there to apply multiselect functionlaity. So first update the option list by AJAX call then initiate the multiselect call you will get the dropdown list with the dynamic option list.
Hope this will help you out.
Multiselect dropdown list and related js & css files
// This function should be called while loading page_x000D_
var loadParentTaskList = function(){_x000D_
$.ajax({_x000D_
url: yoururl,_x000D_
method: 'POST',_x000D_
success: function(data){_x000D_
// To add options list coming from AJAX call multiselect_x000D_
for (var field in data) {_x000D_
$('<option value = "'+ data[field].name +'">' + data[field].name + '</option>').appendTo('#parent_task');_x000D_
}_x000D_
_x000D_
// To initiate the multiselect call _x000D_
$("#parent_task").multiselect({_x000D_
includeSelectAllOption: true_x000D_
})_x000D_
}_x000D_
});_x000D_
}
_x000D_
// Multiselect drop down list with id parent_task_x000D_
<select id="parent_task" multiple="multiple">_x000D_
</select>
_x000D_
You cannot place checkbox inside select element but you can get the same functionality by using HTML, CSS and JavaScript. Here is a possible working solution. The explanation follows.
var expanded = false;_x000D_
_x000D_
function showCheckboxes() {_x000D_
var checkboxes = document.getElementById("checkboxes");_x000D_
if (!expanded) {_x000D_
checkboxes.style.display = "block";_x000D_
expanded = true;_x000D_
} else {_x000D_
checkboxes.style.display = "none";_x000D_
expanded = false;_x000D_
}_x000D_
}
_x000D_
.multiselect {_x000D_
width: 200px;_x000D_
}_x000D_
_x000D_
.selectBox {_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
.selectBox select {_x000D_
width: 100%;_x000D_
font-weight: bold;_x000D_
}_x000D_
_x000D_
.overSelect {_x000D_
position: absolute;_x000D_
left: 0;_x000D_
right: 0;_x000D_
top: 0;_x000D_
bottom: 0;_x000D_
}_x000D_
_x000D_
#checkboxes {_x000D_
display: none;_x000D_
border: 1px #dadada solid;_x000D_
}_x000D_
_x000D_
#checkboxes label {_x000D_
display: block;_x000D_
}_x000D_
_x000D_
#checkboxes label:hover {_x000D_
background-color: #1e90ff;_x000D_
}
_x000D_
<form>_x000D_
<div class="multiselect">_x000D_
<div class="selectBox" onclick="showCheckboxes()">_x000D_
<select>_x000D_
<option>Select an option</option>_x000D_
</select>_x000D_
<div class="overSelect"></div>_x000D_
</div>_x000D_
<div id="checkboxes">_x000D_
<label for="one">_x000D_
<input type="checkbox" id="one" />First checkbox</label>_x000D_
<label for="two">_x000D_
<input type="checkbox" id="two" />Second checkbox</label>_x000D_
<label for="three">_x000D_
<input type="checkbox" id="three" />Third checkbox</label>_x000D_
</div>_x000D_
</div>_x000D_
</form>
_x000D_
At first we create a select element that shows text "Select an option", and empty element that covers (overlaps) the select element (<div class="overSelect">
). We do not want the user to click on the select element - it would show an empty options. To overlap the element with other element we use CSS position property with value relative | absolute.
To add the functionality we specify a JavaScript function that is called when the user clicks on the div that contains our select element (<div class="selectBox" onclick="showCheckboxes()">
).
We also create div that contains our checkboxes and style it using CSS. The above mentioned JavaScript function just changes <div id="checkboxes">
value of CSS display property from "none" to "block" and vice versa.
The solution was tested in the following browsers: Internet Explorer 10, Firefox 34, Chrome 39. The browser needs to have JavaScript enabled.
CSS positioning
How to overlay one div over another div
http://www.w3schools.com/css/css_positioning.asp
CSS display property
I started from @vitfo answer but I want to have <option>
inside <select>
instead of checkbox inputs so i put together all the answers to make this, there is my code, I hope it will help someone.
$(".multiple_select").mousedown(function(e) {_x000D_
if (e.target.tagName == "OPTION") _x000D_
{_x000D_
return; //don't close dropdown if i select option_x000D_
}_x000D_
$(this).toggleClass('multiple_select_active'); //close dropdown if click inside <select> box_x000D_
});_x000D_
$(".multiple_select").on('blur', function(e) {_x000D_
$(this).removeClass('multiple_select_active'); //close dropdown if click outside <select>_x000D_
});_x000D_
_x000D_
$('.multiple_select option').mousedown(function(e) { //no ctrl to select multiple_x000D_
e.preventDefault(); _x000D_
$(this).prop('selected', $(this).prop('selected') ? false : true); //set selected options on click_x000D_
$(this).parent().change(); //trigger change event_x000D_
});_x000D_
_x000D_
_x000D_
$("#myFilter").on('change', function() {_x000D_
var selected = $("#myFilter").val().toString(); //here I get all options and convert to string_x000D_
var document_style = document.documentElement.style;_x000D_
if(selected !== "")_x000D_
document_style.setProperty('--text', "'Selected: "+selected+"'");_x000D_
else_x000D_
document_style.setProperty('--text', "'Select values'");_x000D_
});
_x000D_
:root_x000D_
{_x000D_
--text: "Select values";_x000D_
}_x000D_
.multiple_select_x000D_
{_x000D_
height: 18px;_x000D_
width: 90%;_x000D_
overflow: hidden;_x000D_
-webkit-appearance: menulist;_x000D_
position: relative;_x000D_
}_x000D_
.multiple_select::before_x000D_
{_x000D_
content: var(--text);_x000D_
display: block;_x000D_
margin-left: 5px;_x000D_
margin-bottom: 2px;_x000D_
}_x000D_
.multiple_select_active_x000D_
{_x000D_
overflow: visible !important;_x000D_
}_x000D_
.multiple_select option_x000D_
{_x000D_
display: none;_x000D_
height: 18px;_x000D_
background-color: white;_x000D_
}_x000D_
.multiple_select_active option_x000D_
{_x000D_
display: block;_x000D_
}_x000D_
_x000D_
.multiple_select option::before {_x000D_
content: "\2610";_x000D_
}_x000D_
.multiple_select option:checked::before {_x000D_
content: "\2611";_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<select id="myFilter" class="multiple_select" multiple>_x000D_
<option>A</option>_x000D_
<option>B</option>_x000D_
<option>C</option>_x000D_
<option>D</option>_x000D_
<option>E</option>_x000D_
</select>
_x000D_
You can try Bootstrap-select. It has a live search too!
For a Pure CSS approach, you can use the :checked
selector combined with the ::before
selector to inline conditional content.
Just add the class select-checkbox
to your select
element and include the following CSS:
.select-checkbox option::before {
content: "\2610";
width: 1.3em;
text-align: center;
display: inline-block;
}
.select-checkbox option:checked::before {
content: "\2611";
}
You can use plain old unicode characters (with an escaped hex encoding) like these:
\2610
\2611
Or if you want to spice things up, you can use these FontAwesome glyphs
\f096
\f046
select {_x000D_
width: 150px;_x000D_
}_x000D_
_x000D_
.select-checkbox option::before {_x000D_
content: "\2610";_x000D_
width: 1.3em;_x000D_
text-align: center;_x000D_
display: inline-block;_x000D_
}_x000D_
.select-checkbox option:checked::before {_x000D_
content: "\2611";_x000D_
}_x000D_
_x000D_
.select-checkbox-fa option::before {_x000D_
font-family: FontAwesome;_x000D_
content: "\f096";_x000D_
width: 1.3em;_x000D_
display: inline-block;_x000D_
margin-left: 2px;_x000D_
}_x000D_
.select-checkbox-fa option:checked::before {_x000D_
content: "\f046";_x000D_
}
_x000D_
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">_x000D_
_x000D_
<h3>Unicode</h3>_x000D_
<select multiple="" class="form-control select-checkbox" size="5">_x000D_
<option>Dog</option>_x000D_
<option>Cat</option>_x000D_
<option>Hippo</option>_x000D_
<option>Dinosaur</option>_x000D_
<option>Another Dog</option>_x000D_
</select>_x000D_
_x000D_
<h3>Font Awesome</h3>_x000D_
<select multiple="" class="form-control select-checkbox-fa" size="5">_x000D_
<option>Dog</option>_x000D_
<option>Cat</option>_x000D_
<option>Hippo</option>_x000D_
<option>Dinosaur</option>_x000D_
<option>Another Dog</option>_x000D_
</select>
_x000D_
If you want to create multiple select dropdowns in the same page:
.multiselect {
width: 200px;
}
.selectBox {
position: relative;
}
.selectBox select {
width: 100%;
font-weight: bold;
}
.overSelect {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
#checkboxes {
display: none;
border: 1px #dadada solid;
}
#checkboxes label {
display: block;
}
#checkboxes label:hover {
background-color: #1e90ff;
}
Html:
<form>
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<label for="one">
<input type="checkbox" id="one" />First checkbox</label>
<label for="two">
<input type="checkbox" id="two" />Second checkbox</label>
<label for="three">
<input type="checkbox" id="three" />Third checkbox</label>
</div>
</div>
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<label for="one">
<input type="checkbox" id="one" />First checkbox</label>
<label for="two">
<input type="checkbox" id="two" />Second checkbox</label>
<label for="three">
<input type="checkbox" id="three" />Third checkbox</label>
</div>
</div>
</form>
Using Jquery:
function showCheckboxes(elethis) {
if($(elethis).next('#checkboxes').is(':hidden')){
$(elethis).next('#checkboxes').show();
$('.selectBox').not(elethis).next('#checkboxes').hide();
}else{
$(elethis).next('#checkboxes').hide();
$('.selectBox').not(elethis).next('#checkboxes').hide();
}
}
Only add class create div and add class form-control. iam use JSP,boostrap4. Ignore c:foreach.
<div class="multi-select form-control" style="height:107.292px;">
<div class="checkbox" id="checkbox-expedientes">
<c:forEach var="item" items="${postulantes}">
<label class="form-check-label">
<input id="options" class="postulantes" type="checkbox" value="1">Option 1</label>
</c:forEach>
</div>
</div>
The best plugin so far is Bootstrap Multiselect
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Multi Select Dropdown with Checkboxes</title>
<link rel="stylesheet" href="css/bootstrap-3.1.1.min.css" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap-multiselect.js"></script>
</head>
<body>
<form id="form1">
<div style="padding:20px">
<select id="chkveg" 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>
<br /><br />
<input type="button" id="btnget" value="Get Selected Values" />
<script type="text/javascript">
$(function() {
$('#chkveg').multiselect({
includeSelectAllOption: true
});
$('#btnget').click(function(){
alert($('#chkveg').val());
});
});
</script>
</div>
</form>
</body>
</html>
Here's the DEMO
$(function() {_x000D_
_x000D_
$('#chkveg').multiselect({_x000D_
includeSelectAllOption: true_x000D_
});_x000D_
_x000D_
$('#btnget').click(function() {_x000D_
alert($('#chkveg').val());_x000D_
});_x000D_
});
_x000D_
.multiselect-container>li>a>label {_x000D_
padding: 4px 20px 3px 20px;_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<script src="https://davidstutz.de/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>_x000D_
<link href="https://davidstutz.de/bootstrap-multiselect/docs/css/bootstrap-3.3.2.min.css" rel="stylesheet"/>_x000D_
<link href="https://davidstutz.de/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" rel="stylesheet"/>_x000D_
<script src="https://davidstutz.de/bootstrap-multiselect/docs/js/bootstrap-3.3.2.min.js"></script>_x000D_
_x000D_
<form id="form1">_x000D_
<div style="padding:20px">_x000D_
_x000D_
<select id="chkveg" multiple="multiple">_x000D_
<option value="cheese">Cheese</option>_x000D_
<option value="tomatoes">Tomatoes</option>_x000D_
<option value="mozarella">Mozzarella</option>_x000D_
<option value="mushrooms">Mushrooms</option>_x000D_
<option value="pepperoni">Pepperoni</option>_x000D_
<option value="onions">Onions</option>_x000D_
</select>_x000D_
_x000D_
<br /><br />_x000D_
_x000D_
<input type="button" id="btnget" value="Get Selected Values" />_x000D_
</div>_x000D_
</form>
_x000D_
You can use this library on git for this purpose https://github.com/ehynds/jquery-ui-multiselect-widget
for initiating the selectbox use this
$("#selectBoxId").multiselect().multiselectfilter();
and when you have the data ready in json (from ajax or any method), first parse the data & then assign the js array to it
var js_arr = $.parseJSON(/*data from ajax*/);
$("#selectBoxId").val(js_arr);
$("#selectBoxId").multiselect("refresh");
Use this code for checkbox list on option menu.
.dropdown-menu input {_x000D_
margin-right: 10px;_x000D_
}
_x000D_
<div class="btn-group">_x000D_
<a href="#" class="btn btn-primary"><i class="fa fa-cogs"></i></a>_x000D_
<a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">_x000D_
<span class="caret"></span>_x000D_
</a>_x000D_
<ul class="dropdown-menu" style="padding: 10px" id="myDiv">_x000D_
<li><p><input type="checkbox" value="id1" > OA Number</p></li>_x000D_
<li><p><input type="checkbox" value="id2" >Customer</p></li>_x000D_
<li><p><input type="checkbox" value="id3" > OA Date</p></li>_x000D_
<li><p><input type="checkbox" value="id4" >Product Code</p></li>_x000D_
<li><p><input type="checkbox" value="id5" >Name</p></li>_x000D_
<li><p><input type="checkbox" value="id6" >WI Number</p></li>_x000D_
<li><p><input type="checkbox" value="id7" >WI QTY</p></li>_x000D_
<li><p><input type="checkbox" value="id8" >Production QTY</p></li>_x000D_
<li><p><input type="checkbox" value="id9" >PD Sr.No (from-to)</p></li>_x000D_
<li><p><input type="checkbox" value="id10" > Production Date</p></li>_x000D_
<button class="btn btn-info" onClick="showTable();">Go</button>_x000D_
</ul>_x000D_
</div>
_x000D_
Source: Stackoverflow.com