Skip to content
Advertisement

How to detect when a radio input is unchecked in Javascript

I have 4 radio input separated by divs. I want to select the input, and make the card border change. But the event listener only works when it’s checked, no when it’s unchecked.

Codepen version https://codepen.io/nazarenoalt/pen/dyOKqYp

JavaScript

HTML

JavaScript

Advertisement

Answer

You can maintain a reference to the last selected

JavaScript
JavaScript

I would just add and remove a class so you do not have to maintain a reference to the last selected element.

JavaScript
JavaScript
JavaScript

If you alter the code a bit, you can just do this all in CSS

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement