Skip to content

Commit 834e5f3

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 6bf88fd commit 834e5f3

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
document.addEventListener( 'change', function ( event ) {
15+
16+
if ( event.target.type == 'radio' ) {
17+
const selectedRadio = event.target;
18+
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+
}
24+
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}"]` );
33+
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+
}
41+
}
42+
});

0 commit comments

Comments
 (0)