Skip to content

Commit acf83c9

Browse files
committed
Fix missing value in dropdowns with conditional options
1 parent 3a9b158 commit acf83c9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

public/js/toggle.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33
*/
44
export function initToggles() {
55
/**
6-
* Change handler for the trigger element: updates state of options and clears the selected value.
6+
* Change handler for the trigger element: updates state of options and clears the parent value.
77
*
88
* @param option
99
* @param parent parent element containing all options
1010
* @param triggerElement
1111
* @param toggleValue specific to this conditional option
12+
* @param {boolean} clear clearing the parent value is optional
1213
*/
13-
function updateConditionalOptions(option, parent, triggerElement, toggleValue) {
14+
function updateConditionalOptions(option, parent, triggerElement, toggleValue, clear = true) {
1415
if (toggleValue === getFormInputValue(triggerElement)) {
1516
option.removeAttribute('disabled');
1617
option.removeAttribute('hidden');
1718
} else {
1819
option.setAttribute('disabled', true);
1920
option.setAttribute('hidden', true);
20-
option.setAttribute('selected', false);
21-
option.setAttribute('checked', false);
21+
option.removeAttribute('selected');
22+
option.removeAttribute('checked');
23+
}
24+
if (clear) {
25+
clearFormElementValue(parent);
2226
}
23-
clearFormElementValue(parent);
2427
}
2528

2629
const fieldsetsWithToggle = document.querySelectorAll('fieldset[data-toggle-id]');
@@ -53,7 +56,8 @@ export function initToggles() {
5356
Array.from(toggleValues). forEach(function(toggleValue) {
5457
const triggerElement = getToggleFormInput(toggleId, toggleValue);
5558
if (triggerElement) {
56-
updateConditionalOptions(option, parent, triggerElement, toggleValue);
59+
// prevent clearing the value on initial load
60+
updateConditionalOptions(option, parent, triggerElement, toggleValue, false);
5761

5862
triggerElement.addEventListener('change', function (e) {
5963
updateConditionalOptions(option, parent, triggerElement, toggleValue);

0 commit comments

Comments
 (0)