Programatically selecting options in a multi-select dropdown [RESOLVED]

Hello!

I am trying to automatically select certain values in a multi-select form based on the user’s selection of other options in the multi-select field.

For example, if they select option A in the multi-select, I want to automatically also select options B and C for the user in the same multi-select field. I’m struggling to find a way to do this.

Any suggestions would be appreciated. Thanks!

Hi Austin,

Unfortunately, this is not a supported feature and can only be done using custom code, can you please share the form and the logic of how the selections should be made? I can send you a code snippet that will help.

Regards,
Sherif

Hi @sherif,

Thanks so much for the reply. Here is a link to the form: Become a Member | Wisconsin Cancer Collaborative

The last field “Which priorities…” is the field in question. We’d want all of the priorities under a chapter selected if the select the chapter. So, for example, if they select Chapter 1, it would also select priorities 1, 2 and 3 under chapter 1.

Let me know if you need anything else. Appreciate the example code snippet.

Thanks for the information Austin, I will get back to you with the code snippet by the end of today.

1 Like

Hi Austin, Sorry for not replying sooner!

Can you please try this

jQuery('#input_3_38').on( 'change', function( e, params ) {
	
	if ( params && 'deselected' in params && params.deselected ) {
		return;
	}

    var valueMap = { '9114':[ '9174', '9176', '9175' ] };
    var clickedValue = e.target.value;
    var allSelectedValues = [];

    var map = Object.keys( valueMap ).includes( clickedValue ) ? valueMap[ clickedValue ] : false;

    if ( map === false ) {
    	return;
    }

	allSelectedValues = allSelectedValues.concat( map );
	allSelectedValues.push( clickedValue );

	jQuery( this ).val( allSelectedValues ).trigger( 'chosen:updated' );
});

This should work for chapter one only, If that is what you need, I will just add the other values to the valueMap var and it should work for the other chapters as well.

Take a look at this article to know where to put this code snippet, if you need help with this as well just let me know!

1 Like

Thanks @sherif! This is working great for the first chapter. Appreciate all the help!

I’m glad that helped, I will update the code to include the rest of the chapters soon.

Thanks Sherif. I was actually able to get the rest of the chapters added to the code myself, so no need for you to do the extra works.

1 Like