I'm having an issue with radio inputs and controlling them through JavaScript. The problem I have is with evaluating whether an input is checked when the click event fires.
const radio = document.querySelector('input[type="radio"]');
radio.addEventListener('click', (e) => {
console.log(e.target.checked);
console.log(e)
if(e.target.checked){
e.target.checked = false;
}
});
<label for="radio">Click me to trigger event</label><input type="radio" id="radio" name="radio">
<label for="radio2">Click me to switch back</label><input type="radio" id="radio2" name="radio">
This shows how if you trigger the event, there is a mismatch between the e.target.checked value and if you inspect the MouseEvent object in the browser console (navigate to the target and the checked property). When you compare the two, the former always appear to be true (even when visually you can see the input is not checked) and the latter is false.
Why are the two properties not the same? Any help will be appreciated.