Skip to content

Commit 3a9b158

Browse files
committed
Fix bug in toggles with multiple triggers
1 parent 93f5448 commit 3a9b158

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

public/js/toggle.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export function initToggles() {
1010
* @param triggerElement
1111
* @param toggleValue specific to this conditional option
1212
*/
13-
function resetConditional(option, parent, triggerElement, toggleValue) {
14-
if (toggleValue.includes(getFormInputValue(triggerElement))) {
13+
function updateConditionalOptions(option, parent, triggerElement, toggleValue) {
14+
if (toggleValue === getFormInputValue(triggerElement)) {
1515
option.removeAttribute('disabled');
1616
option.removeAttribute('hidden');
1717
} else {
@@ -29,36 +29,37 @@ export function initToggles() {
2929
// Init fieldset states on page load and add event listeners
3030
Array.from(fieldsetsWithToggle).forEach(function(fieldset) {
3131
const toggleId = fieldset.getAttribute('data-toggle-id');
32-
const toggleValue = JSON.parse(fieldset.getAttribute('data-toggle-value'));
33-
const formInput = getToggleFormInput(toggleId, toggleValue);
34-
if (formInput) {
35-
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
36-
formInput.addEventListener('change', function(e) {
37-
const formInput = e.target;
38-
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
39-
});
40-
}
32+
const toggleValues = JSON.parse(fieldset.getAttribute('data-toggle-value'));
33+
// loop over toggleValues
34+
Array.from(toggleValues). forEach(function(toggleValue) {
35+
const triggerElement = getToggleFormInput(toggleId, toggleValue);
36+
if (triggerElement) {
37+
toggleFieldset(fieldset, triggerElement, triggerElement.checkVisibility());
38+
triggerElement.addEventListener('change', function(e) {
39+
const formInput = e.target;
40+
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
41+
});
42+
}
43+
});
4144
});
4245

4346
// Init state of conditional select options on page load
4447
// and add change handler
4548
Array.from(optionsWithToggle).forEach(function(option) {
4649
const parent = option.parentElement;
4750
const toggleId = option.getAttribute('data-toggle-id');
48-
const toggleValue = JSON.parse(option.getAttribute('data-toggle-value'));
49-
const triggerElement = getToggleFormInput(toggleId, toggleValue);
50-
if (triggerElement) {
51-
if (toggleValue.includes(getFormInputValue(triggerElement))) {
52-
option.removeAttribute('disabled');
53-
option.removeAttribute('hidden');
54-
} else {
55-
option.removeAttribute('selected');
56-
}
51+
const toggleValues = JSON.parse(option.getAttribute('data-toggle-value'));
52+
// loop over toggleValues
53+
Array.from(toggleValues). forEach(function(toggleValue) {
54+
const triggerElement = getToggleFormInput(toggleId, toggleValue);
55+
if (triggerElement) {
56+
updateConditionalOptions(option, parent, triggerElement, toggleValue);
5757

58-
triggerElement.addEventListener('change', function(e) {
59-
resetConditional(option, parent, triggerElement, toggleValue);
60-
});
61-
}
58+
triggerElement.addEventListener('change', function (e) {
59+
updateConditionalOptions(option, parent, triggerElement, toggleValue);
60+
});
61+
}
62+
});
6263
});
6364

6465
// clear all inputs in hidden fields on submit
@@ -87,17 +88,15 @@ export function initToggles() {
8788
if (isVisible && toggleValue.includes(getFormInputValue(formInput))) {
8889
fieldset.removeAttribute('disabled');
8990
fieldset.classList.remove('hidden');
90-
Array.from(fieldset.querySelectorAll('.form-input')).forEach(function(fieldsetFormElement) {
91-
fieldsetFormElement.dispatchEvent(new Event("change"));
92-
});
9391
} else {
9492
fieldset.setAttribute('disabled', '');
9593
fieldset.classList.add('hidden');
96-
Array.from(fieldset.querySelectorAll('.form-input')).forEach(function(fieldsetFormElement) {
97-
// Trigger event in case another fieldset is affected due to this toggle
98-
fieldsetFormElement.dispatchEvent(new Event("change"));
99-
});
10094
}
95+
96+
Array.from(fieldset.querySelectorAll('.form-input')).forEach(function(fieldsetFormElement) {
97+
// Trigger event in case another fieldset is affected due to this toggle
98+
fieldsetFormElement.dispatchEvent(new Event("change"));
99+
});
101100
}
102101

103102
// Helper function to get form input
@@ -112,9 +111,10 @@ export function initToggles() {
112111
} else {
113112
let divFormInput;
114113
Array.from(formInput.querySelectorAll('.form-input')).forEach(function(formElement) {
115-
if (toggleValue.includes(formElement.value)) {
114+
if (toggleValue === formElement.value) {
116115
divFormInput = formElement;
117-
} else if (formElement.type === 'radio') {
116+
}
117+
if (formElement.type === 'radio') {
118118
formElement.addEventListener('click', function(e) {
119119
divFormInput.dispatchEvent(new Event("change"));
120120
});
@@ -125,6 +125,12 @@ export function initToggles() {
125125
}
126126
}
127127

128+
function attachToggleHandlerToInput(divFormInput, formElement) {
129+
formElement.addEventListener('click', function(e) {
130+
divFormInput.dispatchEvent(new Event("change"));
131+
});
132+
}
133+
128134
// Helper function to get input value
129135
// This function is necessary for radios and checkboxes as they always have a value
130136
// which gets only returned if this formInput is checked

0 commit comments

Comments
 (0)