At first it is not possible to add checkboxes to select element directly. You need to use js for this purpose. There are basically two ways how to do "select" dropdown list with checkboxes:
- use
select
and create dropdown menu in another div
element with inputs
(type checkbox) which will be showed instead of original select
options
- as in my example, I am using two
divs
, one is for dropdown list (consists from inputs
type checkbox) and one is for showing results.
Second approach is better according to my experiences bcz when you send form, it will send just input
values. In first option it will send also select
value.
I make sample in jquery for your reference.
HTML/PHP code:
<div class="form__select-wrapper form__select-wrapper--multiselect">
<div class="form__select form__select--multiselect" required>Vyberte zdravotnú poisťovňu *</div>
<i class="arrow"></i>
<div class="form__select-dropdown">
<?php foreach ($insurances as $insurance) : ?>
<label for="<?= plainText($insurance['insurance_id']); ?>" class="form__label--multiselect">
<input name="insurances[]" type="checkbox" id="<?= plainText($insurance['insurance_id']); ?>" value="<?= plainText($insurance['insurance_id']); ?>" class="form__checkbox" /> <?= plainText($insurance['insurance_name']); ?>
</label>
<?php endforeach ?>
</div>
</div>
</div>
CSS code:
.form__label--multiselect {
display: block;
padding: 0.2em;
background-color: #e0f4ff;
color: #888;
}
.form__label--multiselect:hover {
background-color: #ddd;
color: #555;
}
.form__select-dropdown {
display: none;
position: absolute;
width: 100%;
border: 0.5px #7c7c7c solid;
background-color: #ffffff;
font-family: 'Sofia Sans', sans-serif;
max-height: 150px;
overflow-y: scroll;
z-index: 999;
}
.form__select-wrapper {
position: relative;
margin-top: 1rem;
}
jQuery code:
// Function to show or hide multiselect list
function toggleCheckboxArea(onlyHide = false) {
var checkboxes = $('.form__select-dropdown');
var displayValue = checkboxes.css('display');
if (displayValue !== 'block') {
if (onlyHide) {
checkboxes.css({display: 'block'});
}
} else {
checkboxes.css('display', 'none');
}
}
// Function to get cheked options from multiselect list with checkboxes and show in div textarea
function handleMultiSelectChange() {
var currentLabel = $(this),
currentCheckedInputs = currentLabel.parent().find('input:checked'),
currentOption = currentLabel.find('option')[0],
currentInputText = currentLabel.text(),
currentDiv = currentLabel.parent().prev().prev(),
selectedInsurances = [];
// Check all checkboxes and add texts to array selectedInsurances
currentCheckedInputs.each(function() {
var insuranceName = $(this).parent().text().trim().split(' ')[0],
fullInsuranceName = $(this).parent().text().trim();
if (fullInsuranceName === 'Bez zmluvnej poisťovňe') {
// Add full text to array
selectedInsurances.push(fullInsuranceName);
} else {
// Add text to array
selectedInsurances.push(insuranceName);
}
});
// Show text form checkboxes in div
if (currentCheckedInputs.length !== 0) {
// Show texts from array separated by ;
currentDiv.text(selectedInsurances.join('; '));
currentDiv.css({color: '#555'});
} else {
// If user choose uncheck all checkboxes return default text
currentDiv.text('Vyberte zdravotnú poisťovňu *');
currentDiv.css({color: '#aaa'});
}
};
// Event listener for change
multiSelectLabel.on('change', handleMultiSelectChange);
// Event listener pre page reloading
$(window).on('load', function() {
// Make action after page loading
multiSelectLabel.trigger('change');
});
// Show dropdown menu if click on div
$('.form__select--multiselect').on('click', function(event) {
event.preventDefault();
var multiSelectLabel = $('.form__label--multiselect'),
currentDivSelect = $(this),
multiSelectCheckbox = $('.form__checkbox'),
targetElement = event.target; // Get target element in html text form
// Hide and show dropdown list just if it is not clicked on dorpdown list directly
if ((targetElement.className !== multiSelectCheckbox.attr('class')) && (targetElement.className !== multiSelectLabel.attr('class'))) {
toggleCheckboxArea(true);
}
// Change color of div on click
if (currentDivSelect.css("background-color") == fieldsBackgroundColor) {
currentDivSelect.css({backgroundColor: 'white'});
} else {
currentDivSelect.css({backgroundColor: '#e0f4ff'});
}
});
// Hide dropdown menu if click outside of select
$(document).on('mousedown', function() {
var multiSelectDropdown = $('.form__select-dropdown'),
multiSelectTextArea = $('.form__select--multiselect'),
multiSelectLabel = $('.form__label--multiselect'),
multiSelectCheckbox = $('.form__checkbox'),
currentSelectArrow = multiSelectTextArea.parent().find('.arrow');
targetElement = event.target; // Get target element in html text form
if (multiSelectDropdown.is(':visible') && (targetElement.className !== multiSelectCheckbox.attr('class')) && (targetElement.className !== multiSelectLabel.attr('class')) && (targetElement.className !== multiSelectTextArea.attr('class'))) {
toggleCheckboxArea(true);
}
});