Skip to content

Commit e4407f9

Browse files
authored
Create gw-prevent-duplicate-selections.js
1 parent 94e06a1 commit e4407f9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Gravity Wiz // Gravity Forms // Prevent Duplicate Selections
3+
* https://gravitywiz.com/
4+
*
5+
* Prevent duplicate selections in choice-based fields. Currently limited to Checkbox fields with plans
6+
* to support all choice-based fields (e.g. Radio Buttons, Drop Downs & Multi Selects).
7+
*
8+
* For Drop Down field support, use this snippet:
9+
* https://github.com/gravitywiz/snippet-library/blob/master/experimental/gw-prevent-duplicate-drop-down-selections.js
10+
*
11+
* Instructions:
12+
*
13+
* 1. Install this snippet with our free Custom JavaScript plugin.
14+
* https://gravitywiz.com/gravity-forms-custom-javascript/
15+
*
16+
* 2. Add 'gw-prevent-duplicates' to the CSS Class Name setting for any field in which duplicate selections
17+
* should be prevented.
18+
*
19+
$checkboxes = $( '.gw-prevent-duplicates' ).find( 'input' );
20+
21+
$checkboxes.click( function() {
22+
gwDisableDuplicates( $( this ), $checkboxes );
23+
} );
24+
25+
$checkboxes.each( function() {
26+
gwDisableDuplicates( $( this ), $checkboxes );
27+
} );
28+
29+
function gwDisableDuplicates( $elem, $group ) {
30+
let value = $elem.val();
31+
$group
32+
.not( $elem )
33+
.filter( '[value="{0}"]'.format( value ) )
34+
.prop( 'disabled', $elem.is( ':checked' ) );
35+
}

0 commit comments

Comments
 (0)