Skip to content

Commit 281b694

Browse files
authored
gw-limit-columns-in-survey-field-to-single-selection.js: Added snippet to limit columns in survey field to single selections.
1 parent 2bd6e83 commit 281b694

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Gravity Wiz // Gravity Forms // Limit Columns in Survey Field to Single Selection
3+
* https://gravitywiz.com/
4+
*
5+
* Video: https://www.loom.com/share/cf1f7f5bb254430c8ae939d5d4b9ea20
6+
*
7+
* Instructions:
8+
*
9+
* 1. Install this snippet with our free Custom JavaScript plugin.
10+
* https://gravitywiz.com/gravity-forms-code-chest/
11+
*/
12+
// Update to the Survey field ID on your form.
13+
const fieldId = '1';
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';
17+
18+
$( document ).on( 'change', `#field_${GFFORMID}_${fieldId} input[type="radio"]`, function () {
19+
const $selectedRadio = $(this);
20+
const $td = $selectedRadio.closest('td');
21+
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_' ) ) ;
29+
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}"]` );
34+
35+
// Deselect all other radio buttons in the same column.
36+
$radiosInColumn.not( $selectedRadio ).prop( 'checked', false );
37+
}
38+
});

0 commit comments

Comments
 (0)