Explain Codes LogoExplain Codes Logo

How to make a radio button unchecked by clicking it?

javascript
event-delegation
javascript-features
accessibility
Anton ShumikhinbyAnton Shumikhin·Nov 27, 2024
TLDR

DoubleClick allows radio button unchecked action. Using JavaScript onClick event handler, track the last clicked button. If it matches the currently clicked button, uncheck it.

let pal, weHadGoodTimes; document.addEventListener('click', function(e) { if (e.target.type === 'radio') { // are you the one? if (e.target === pal) { // end of the road, pal e.target.checked = false; weHadGoodTimes = null; } else { // excuse me, are you my pal? weHadGoodTimes = e.target; } } });

Add this script to your HTML after the radio buttons to jazz up the functionality.

Understand the click event

Click event handlers in JavaScript are crucial to deliver this functionality. Below are advancements to fit different contexts.

The street performer: dynamic elements

Dynamic elements in the DOM require sharp vigilance. To manage dynamic elements, event delegation in JavaScript is your best friend, basically the bodyguard hiring you!

document.addEventListener('click', function(e) { if (e.target.matches('input[type="radio"]')) { // Event delegation logic here } });

The piano player: key press

Did you ever try to play a piano without Ctrl or Cmd keys? Feels like you're playing without any black keys, huh? Let's not do that.

document.addEventListener('click', function(e) { if (e.ctrlKey || e.metaKey) { // Logic to play a beautiful symphony of user interaction } });

The family tree: form element association

The relationship between a radio button and its form is as poetic as Romeo and Juliet. Thanks to .closest().

let radio = e.target; let form = radio.closest('form'); // Time to meet the in-laws

The reflection in the mirror: accessibility

Make sure to scream change whenever the checked value is modified, because who does love a good surprise in programming?

e.target.checked = false; e.target.dispatchEvent(new Event('change')); // Avoid surprised looks

The optimal option: "No answer"

The good ol' “I don't know” sometimes is the best answer. A clear and cautious option to deselect is to include a "none" option:

<label><input type="radio" name="option" value=""> I'd rather not say </label>

Overcoming challenges

Memory lane: using data attributes

Memory is everything in coding. Bend it to your will using data attributes, keeping track of the checked state:

document.addEventListener('click', function(e) { // Introspective logic right here });

Lone wolf: single radio inputs

Your jQuery juice needs to cover each radio button individually:

$('input[type="radio"]').each(function() { // Unique logic to each sparkly-eyed radio button });

The amoeba: dynamic radio button groups

Like amoeba, dynamic part of the code can divide and conquer our attention. Excluding the active button is a fine art:

$('input[name="group"]').not(':checked').prop('checked', true);

Magic carpet: keyboard shortcuts

Carpets fly not just in movies, but also in keyboard events. Add a little Aladdin's magic carpet ride:

document.addEventListener('keydown', function(e) { if (e.code === 'Space') { // Open sesame! Logic here } });