Skip to content

Commit 5a06dd0

Browse files
committed
gw-limit-columns-in-survey-field-to-single-selection.js: Added snippet to limit columns in survey field to single selections.
1 parent 834e5f3 commit 5a06dd0

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

gravity-forms/gw-limit-columns-in-survey-field-to-single-selection.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,28 @@
1111
*/
1212
// Update to the Survey field ID on your form.
1313
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';
1517

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');
1821

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_' ) ) ;
2429

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}"]` );
3334

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 );
4137
}
4238
});

0 commit comments

Comments
 (0)