Skip to content

Commit

Permalink
Fix JS errors when toggle dependencies cannot be resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
annda committed Sep 10, 2024
1 parent a1de753 commit 9107aa0
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions public/js/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ export function initToggles() {
const toggleId = fieldset.getAttribute('data-toggle-id');
const toggleValue = JSON.parse(fieldset.getAttribute('data-toggle-value'));
const formInput = getToggleFormInput(toggleId, toggleValue);
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
formInput.addEventListener('change', function(e) {
const formInput = e.target;
if (formInput) {
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
});
formInput.addEventListener('change', function(e) {
const formInput = e.target;
toggleFieldset(fieldset, formInput, formInput.checkVisibility());
});
}
});

// Init state of conditional select options on page load
Expand All @@ -45,17 +47,18 @@ export function initToggles() {
const toggleId = option.getAttribute('data-toggle-id');
const toggleValue = JSON.parse(option.getAttribute('data-toggle-value'));
const triggerElement = getToggleFormInput(toggleId, toggleValue);
if (triggerElement) {
if (toggleValue.includes(getFormInputValue(triggerElement))) {
option.removeAttribute('disabled');
option.removeAttribute('hidden');
} else {
option.removeAttribute('selected');
}

if (toggleValue.includes(getFormInputValue(triggerElement))) {
option.removeAttribute('disabled');
option.removeAttribute('hidden');
} else {
option.removeAttribute('selected');
triggerElement.addEventListener('change', function(e) {
resetConditional(option, parent, triggerElement, toggleValue);
});
}

triggerElement.addEventListener('change', function(e) {
resetConditional(option, parent, triggerElement, toggleValue);
});
});

// clear all inputs in hidden fields on submit
Expand Down Expand Up @@ -101,6 +104,9 @@ export function initToggles() {
// This function is necessary for radios and checkboxes as they are special
function getToggleFormInput(toggleId, toggleValue) {
const formInput = document.getElementById(toggleId);
if (!formInput) {
return null;
}
if (formInput.tagName.toLowerCase() !== 'div') {
return formInput;
} else {
Expand Down

0 comments on commit 9107aa0

Please sign in to comment.