<p>This is just for the demonstration purpose. In such case a better option would be the radio button grouped with the same name and value attribute.</p>
<details id="multiselect">
<summary>Your favourite cars list</summary>
<form>
<fieldset>
<legend>
Cars
</legend>
<ul>
<li>
<label for="bmw">BMW</label>
<input type="checkbox" id="bmw" name="car[]" value="bmw" />
</li>
<li>
<label for="citroen">Citroen</label>
<input type="checkbox" id="citroen" name="car[]" value="citroen" />
</li>
<li>
<label for="skoda">Skoda</label>
<input type="checkbox" id="skoda" name="car[]" value="skoda" />
</li>
<li>
<label for="volvo">Volvo</label>
<input type="checkbox" id="volvo" name="car[]" value="volvo" />
</li>
</ul>
</fieldset>
</form>
</details>
details {
border: 1px solid #767676;
border-radius: 3px;
display: inline-flex;
flex-direction: column;
padding: 3px 6px;
}
details summary::marker {
display: none;
font-size: 0;
}
details summary::-webkit-details-marker {
display: none;
font-size: 0;
}
details summary::after {
content: "\25BC" / "";
display: inline-block;
font-size: 0.6rem;
height: 1rem;
line-height: 1rem;
margin-left: 0.5rem;
position: relative;
transition: transform 0.25s;
}
details[open] summary {
margin-bottom: 1rem;
}
details[open] summary::after {
top: -0.15rem;
transform: rotate(180deg);
}
form {
display: flex;
}
fieldset {
border: 0;
padding: 0;
}
fieldset legend {
position: absolute !important;
width: 1px !important;
height: 1px !important;
padding: 0 !important;
margin: -1px !important;
overflow: hidden !important;
clip: rect(0, 0, 0, 0) !important;
white-space: nowrap !important;
border: 0 !important;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
ul li {
border-radius: 3px;
margin: 0;
padding: 4px 2px;
}
ul li:hover {
background: #eee;
}
ul li label {
display: inline-block;
width: 8rem;
}
const multiselectElement = document.querySelector('#multiselect');
const handleEscape = (event) => {
const eventTarget = event.target;
let key;
if (typeof Object.getOwnPropertyDescriptor(KeyboardEvent.prototype, 'code') === 'object') {
key = event.key;
} else if (typeof Object.getOwnPropertyDescriptor(KeyboardEvent.prototype, 'key') === 'object') {
key = event.key;
}
if (key !== 'Escape') {
return;
}
const detailsElement = eventTarget.closest('details');
detailsElement.removeAttribute('open');
detailsElement.querySelector('summary').focus();
};
multiselectElement.addEventListener('keydown', handleEscape);
const allowOnlyOneCheckbox = (event) => {
const eventTarget = event.target;
if (eventTarget.nodeName.toLowerCase() !== 'input') {
return;
}
const allCheckboxes = eventTarget.form.querySelectorAll('input');
Array.from(allCheckboxes).forEach((inputElement) => {
inputElement.checked = false;
});
eventTarget.checked = true;
};
multiselectElement.addEventListener('change', allowOnlyOneCheckbox);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.