|
3 | 3 | */ |
4 | 4 | export function initToggles() { |
5 | 5 | /** |
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. |
7 | 7 | * |
8 | 8 | * @param option |
9 | 9 | * @param parent parent element containing all options |
10 | 10 | * @param triggerElement |
11 | 11 | * @param toggleValue specific to this conditional option |
| 12 | + * @param {boolean} clear clearing the parent value is optional |
12 | 13 | */ |
13 | | - function updateConditionalOptions(option, parent, triggerElement, toggleValue) { |
| 14 | + function updateConditionalOptions(option, parent, triggerElement, toggleValue, clear = true) { |
14 | 15 | if (toggleValue === getFormInputValue(triggerElement)) { |
15 | 16 | option.removeAttribute('disabled'); |
16 | 17 | option.removeAttribute('hidden'); |
17 | 18 | } else { |
18 | 19 | option.setAttribute('disabled', true); |
19 | 20 | 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); |
22 | 26 | } |
23 | | - clearFormElementValue(parent); |
24 | 27 | } |
25 | 28 |
|
26 | 29 | const fieldsetsWithToggle = document.querySelectorAll('fieldset[data-toggle-id]'); |
@@ -53,7 +56,8 @@ export function initToggles() { |
53 | 56 | Array.from(toggleValues). forEach(function(toggleValue) { |
54 | 57 | const triggerElement = getToggleFormInput(toggleId, toggleValue); |
55 | 58 | if (triggerElement) { |
56 | | - updateConditionalOptions(option, parent, triggerElement, toggleValue); |
| 59 | + // prevent clearing the value on initial load |
| 60 | + updateConditionalOptions(option, parent, triggerElement, toggleValue, false); |
57 | 61 |
|
58 | 62 | triggerElement.addEventListener('change', function (e) { |
59 | 63 | updateConditionalOptions(option, parent, triggerElement, toggleValue); |
|
0 commit comments