@@ -10,8 +10,8 @@ export function initToggles() {
10
10
* @param triggerElement
11
11
* @param toggleValue specific to this conditional option
12
12
*/
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 ) ) {
15
15
option . removeAttribute ( 'disabled' ) ;
16
16
option . removeAttribute ( 'hidden' ) ;
17
17
} else {
@@ -29,36 +29,37 @@ export function initToggles() {
29
29
// Init fieldset states on page load and add event listeners
30
30
Array . from ( fieldsetsWithToggle ) . forEach ( function ( fieldset ) {
31
31
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
+ } ) ;
41
44
} ) ;
42
45
43
46
// Init state of conditional select options on page load
44
47
// and add change handler
45
48
Array . from ( optionsWithToggle ) . forEach ( function ( option ) {
46
49
const parent = option . parentElement ;
47
50
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 ) ;
57
57
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
+ } ) ;
62
63
} ) ;
63
64
64
65
// clear all inputs in hidden fields on submit
@@ -87,17 +88,15 @@ export function initToggles() {
87
88
if ( isVisible && toggleValue . includes ( getFormInputValue ( formInput ) ) ) {
88
89
fieldset . removeAttribute ( 'disabled' ) ;
89
90
fieldset . classList . remove ( 'hidden' ) ;
90
- Array . from ( fieldset . querySelectorAll ( '.form-input' ) ) . forEach ( function ( fieldsetFormElement ) {
91
- fieldsetFormElement . dispatchEvent ( new Event ( "change" ) ) ;
92
- } ) ;
93
91
} else {
94
92
fieldset . setAttribute ( 'disabled' , '' ) ;
95
93
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
- } ) ;
100
94
}
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
+ } ) ;
101
100
}
102
101
103
102
// Helper function to get form input
@@ -112,9 +111,10 @@ export function initToggles() {
112
111
} else {
113
112
let divFormInput ;
114
113
Array . from ( formInput . querySelectorAll ( '.form-input' ) ) . forEach ( function ( formElement ) {
115
- if ( toggleValue . includes ( formElement . value ) ) {
114
+ if ( toggleValue === formElement . value ) {
116
115
divFormInput = formElement ;
117
- } else if ( formElement . type === 'radio' ) {
116
+ }
117
+ if ( formElement . type === 'radio' ) {
118
118
formElement . addEventListener ( 'click' , function ( e ) {
119
119
divFormInput . dispatchEvent ( new Event ( "change" ) ) ;
120
120
} ) ;
@@ -125,6 +125,12 @@ export function initToggles() {
125
125
}
126
126
}
127
127
128
+ function attachToggleHandlerToInput ( divFormInput , formElement ) {
129
+ formElement . addEventListener ( 'click' , function ( e ) {
130
+ divFormInput . dispatchEvent ( new Event ( "change" ) ) ;
131
+ } ) ;
132
+ }
133
+
128
134
// Helper function to get input value
129
135
// This function is necessary for radios and checkboxes as they always have a value
130
136
// which gets only returned if this formInput is checked
0 commit comments