First, before you recommend radio buttons, I want to use checkboxes because they can be unchecked. I have 2 custom checkboxes #c1 & #c2; I want one open at a time (so if c1 was open, clicking c2 will uncheck c1); and I want both to be able to be unchecked. Any ideas would be greatly appreciated!
Here's what I've tried:`
let c1 = document.getElementById('c1');
let c2 = document.getElementById('c2');
function oneAtATime() {
if (c1.checked) {
c2.checked = false;
} else if (c2.checked) {
c1.checked = false;
}
}
c1.addEventListener('change', oneAtATime);
c2.addEventListener('change', oneAtATime);
<input id="c1" type="checkbox" name="checkbox" class="check">
<label for="c1">
<h2>example</h2>
</label>
<input id="c1" type="checkbox" name="checkbox" class="check">
<label for="c1">
<h2>example</h2>
</label>
Aucun commentaire:
Enregistrer un commentaire