So before I wrote this code with all my inputs as checkboxes and it worked 100% fine when I click or unclick the options.
My problem is that I changed some of my inputs to radio buttons and now the bottom half of my code doesn't work the same.
If I were to click one a radio button it would show the value of 10, but if I click it again, it will say 20, then I can continue clicking it infinitely and it keeps adding the value to 100000 instead of simply going from 0 to 10 back to 0.
document.addEventListener("click", ({ target }) => {
if (target.className === "class1" && target.checked) {
window.menuItems += parseInt(target.value);
} else if (target.className === "class1" && !target.checked) {
window.menuItems -= parseInt(target.value);
// this logic below for my radio buttons doesn't work the same
} else if (target.className === "radio1" && target.checked) {
window.tip += parseInt(target.value);
} else if (target.className === "radio1" && !target.checked) {
window.tip -= parseInt(target.value);
} else {
return;
}
Is there a built in function to check if a radio input is selected?
Because using target.checked
doesn't work the same for radio buttons and I also tried just target
but that didn't do the same effect.