Skip to content

Commit 0068d03

Browse files
authored
Create gw-limit-multiselect.js
1 parent 060933b commit 0068d03

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Gravity Wiz // Gravity Forms // Limit Multi Selects
3+
* https://gravitywiz.com/
4+
*
5+
* Limit how many options may be selected in a Multi Select field. Works with
6+
* regular Multi Select fields as well as fields with Enhanced UI enabled.
7+
*
8+
* Instructions:
9+
*
10+
* 1. Install this snippet with our free Custom JavaScript plugin.
11+
* https://gravitywiz.com/gravity-forms-custom-javascript/
12+
* 2. Configure snippet per inline instructions.
13+
*/
14+
// Update "1" to the ID of your Multi Select field.
15+
$( '#input_GFFORMID_1' ).on( 'change', function () {
16+
// Update "2" to the max number of options that should be selectable.
17+
var maxSelected = 2;
18+
// Alternate: Set max number by the value of another field.
19+
// var maxSelected = parseInt( $( '#input_GFFORMID_4' ).val() );
20+
limitMultiSelect( $( this ), maxSelected );
21+
} );
22+
23+
function limitMultiSelect( $select, maxSelected ) {
24+
var disable = $select.find( 'option:checked' ).length === maxSelected;
25+
$select
26+
.find( 'option:not(:checked)' )
27+
.prop( 'disabled', disable )
28+
.trigger( 'chosen:updated' );
29+
}

0 commit comments

Comments
 (0)