-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gw-limit-columns-in-survey-field-to-single-selection.js
: Added snip…
…pet to limit columns in survey field to single selections.
- Loading branch information
1 parent
6bf88fd
commit 834e5f3
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
gravity-forms/gw-limit-columns-in-survey-field-to-single-selection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Gravity Wiz // Gravity Forms // Limit Columns in Survey Field to Single Selection | ||
* https://gravitywiz.com/ | ||
* | ||
* Video: https://www.loom.com/share/cf1f7f5bb254430c8ae939d5d4b9ea20 | ||
* | ||
* Instructions: | ||
* | ||
* 1. Install this snippet with our free Custom JavaScript plugin. | ||
* https://gravitywiz.com/gravity-forms-code-chest/ | ||
*/ | ||
// Update to the Survey field ID on your form. | ||
const fieldId = '1'; | ||
document.addEventListener( 'change', function ( event ) { | ||
|
||
if ( event.target.type == 'radio' ) { | ||
const selectedRadio = event.target; | ||
|
||
const field = selectedRadio.closest( `#field_${GFFORMID}_${fieldId}` ); | ||
// Exit if the radio button is not within the target field. | ||
if ( !field ) { | ||
return; | ||
} | ||
|
||
// Get the column ID of the radio button | ||
const ariaLabels = selectedRadio.getAttribute( 'aria-labelledby' ).split(' '); | ||
const columnId = ariaLabels.find(label => label.startsWith( 'likert_col_' )); | ||
|
||
if (columnId) { | ||
// Find all radio buttons in the same table within the specified field/ | ||
const table = selectedRadio.closest( 'table' ); | ||
const radiosInColumn = table.querySelectorAll( `input[type="radio"][aria-labelledby*="${columnId}"]` ); | ||
|
||
// Deselect all other radio buttons in the same column/ | ||
radiosInColumn.forEach(radio => { | ||
if (radio != selectedRadio) { | ||
radio.checked = false; | ||
} | ||
}); | ||
} | ||
} | ||
}); |