|
11 | 11 | */ |
12 | 12 | // Update to the Survey field ID on your form. |
13 | 13 | const fieldId = '1'; |
14 | | -document.addEventListener( 'change', function ( event ) { |
| 14 | +// If you want to exclude a column from this behavior, set the column label here. |
| 15 | +// If you don't want it to exclude any column, set it to an empty string. |
| 16 | +const exceptionColumnLabel = 'Not Available'; |
15 | 17 |
|
16 | | - if ( event.target.type == 'radio' ) { |
17 | | - const selectedRadio = event.target; |
| 18 | +$( document ).on( 'change', `#field_${GFFORMID}_${fieldId} input[type="radio"]`, function () { |
| 19 | + const $selectedRadio = $(this); |
| 20 | + const $td = $selectedRadio.closest('td'); |
18 | 21 |
|
19 | | - const field = selectedRadio.closest( `#field_${GFFORMID}_${fieldId}` ); |
20 | | - // Exit if the radio button is not within the target field. |
21 | | - if ( !field ) { |
22 | | - return; |
23 | | - } |
| 22 | + // Skip logic if the column label matches the exception label. |
| 23 | + if ( exceptionColumnLabel && $td.data('label') == exceptionColumnLabel ) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const ariaLabels = $selectedRadio.attr( 'aria-labelledby' ).split( ' ' ); |
| 28 | + const columnId = ariaLabels.find( label => label.startsWith( 'likert_col_' ) ) ; |
24 | 29 |
|
25 | | - // Get the column ID of the radio button |
26 | | - const ariaLabels = selectedRadio.getAttribute( 'aria-labelledby' ).split(' '); |
27 | | - const columnId = ariaLabels.find(label => label.startsWith( 'likert_col_' )); |
28 | | - |
29 | | - if (columnId) { |
30 | | - // Find all radio buttons in the same table within the specified field/ |
31 | | - const table = selectedRadio.closest( 'table' ); |
32 | | - const radiosInColumn = table.querySelectorAll( `input[type="radio"][aria-labelledby*="${columnId}"]` ); |
| 30 | + if ( columnId ) { |
| 31 | + // Find all radio buttons in the same column. |
| 32 | + const $table = $selectedRadio.closest( 'table' ); |
| 33 | + const $radiosInColumn = $table.find( `input[type="radio"][aria-labelledby*="${columnId}"]` ); |
33 | 34 |
|
34 | | - // Deselect all other radio buttons in the same column/ |
35 | | - radiosInColumn.forEach(radio => { |
36 | | - if (radio != selectedRadio) { |
37 | | - radio.checked = false; |
38 | | - } |
39 | | - }); |
40 | | - } |
| 35 | + // Deselect all other radio buttons in the same column. |
| 36 | + $radiosInColumn.not( $selectedRadio ).prop( 'checked', false ); |
41 | 37 | } |
42 | 38 | }); |
0 commit comments