Skip to content

Commit 0fab5bf

Browse files
committed
Fix an issue with uncheckable radio buttons.
When clicking back and forth between radio buttons in a radio group a radio button can be unexpectedly still unchecked when it should become checked. For example, in a radio group with at least two radio buttons, click on the first radio button. It will now be checked. Now click on the second radio button, and it will now be checked. Now click on the first radio button again. This time it will remain unchecked and the second radio button will also now be unchecked. Another manifestation of this is with keyboard controls. Again with a radio group containing at least two radios. Click on the first radio button (or for a pure keyboard approach navigate to the radio group with tab, and hit the spacebar when the first radio button is focused), and it will now be checked. Now use the down arrow repeatedly. As focus cycles through the radio group buttons on each subsequent full cycle the radios will be all checked, and then all unchecked on the next cycle. The way that the uncheckable radio code works is that javascript adds a data attribute to a radio button when it is checked which indicates that the radio is checked. The problem is that when another radio in the group is checked that second radio now has the data attribute added, but the first does not have the data attribute removed. So if that first radio button is now clicked again the javascript sees the data attribute, deletes it, and undoes the effect of the click and unchecks the radio when in this case it should let the effect of the click occur. To fix the issue, radio groups need to be managed instead of single radio buttons. When the data attribute is added to a radio button, the code needs to ensure the data attribute is removed from the other radio buttons in the group that have it. Of course when a second click occurs on an already checked radio button, that works as before, and the radio is unchecked (and the data attribute removed).
1 parent d115315 commit 0fab5bf

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

htdocs/js/RadioButtons/RadioButtons.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@
1515
'use strict';
1616

1717
(() => {
18+
const radioGroups = {};
19+
1820
// Setup uncheckable radios.
1921
const setupUncheckableRadio = (radio) => {
2022
if (!radio.dataset.uncheckableRadioButton) return;
2123
delete radio.dataset.uncheckableRadioButton;
2224

25+
if (!radioGroups[radio.name]) radioGroups[radio.name] = [radio];
26+
else radioGroups[radio.name].push(radio);
27+
2328
if (radio.checked) radio.dataset.currentlyChecked = '1';
2429

2530
radio.addEventListener('click', (e) => {
31+
for (const groupRadio of radioGroups[radio.name]) {
32+
if (groupRadio === radio) continue;
33+
delete groupRadio.dataset.currentlyChecked;
34+
}
2635
if (radio.dataset.shift && !e.shiftKey) {
2736
radio.dataset.currentlyChecked = '1';
2837
return;
2938
}
30-
const currentlyChecked = radio.dataset.currentlyChecked;
31-
if (currentlyChecked) {
39+
if (radio.dataset.currentlyChecked) {
3240
delete radio.dataset.currentlyChecked;
3341
radio.checked = false;
3442
} else {

0 commit comments

Comments
 (0)