How to hide options based on another and switch it back if other option is deselected?

gunishi
Tera Guru

Hi all, 

 

I have a requirement where on a CI if a user chooses mutligrain bread, they shouldn't be able to pick a strawberry smoothie. 

 

I have an onChange client script that hides the option. I have tried setReadOnly, setVisible, removeOption and all successfully remove the option, however, if the user changes their mind and then clicks on white bread, the strawberry smoothie option doesn't reappear. 

 

How would I go about implementing this? Is the onChange client script in the right direction or is that completely wrong?

Any help would be much appreciated. 

 

Kind regards, 

G

2 ACCEPTED SOLUTIONS

could you please try below code: 


function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

if (newValue == 'multigrain') {

g_form.setVisible('strawberry_smoothie',false);

g_form.setVisible('blueberry_smoothie',false);

if(newValue == 'white bread') {

g_form.setVisible('strawberry_smoothie',true);

g_form.setVisible('blueberry_smoothie',true);

}

}

View solution in original post

gunishi
Tera Guru

Hi there, 

 

Managed to solve this with the help of @chaudharymahesh 

 

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

if (newValue == 'multigrain') {

g_form.setVisible('strawberry_smoothie',false);

g_form.setVisible('blueberry_smoothie',false);

}

if (newValue != 'multigrain'){

g_form.setVisible('strawberry_smoothie', true);

g_form.setVisible('blueberry_smoothie',true);

}

}

 

Hope this helps someone!

View solution in original post

9 REPLIES 9

chaudharymahesh
Mega Sage

do you have the logic to show fields in else part in your client script. if possible, could you share your client script code here

Hi @chaudharymahesh 

 

This is what I have in my script so far: 

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

if (newValue == 'multigrain') {

g_form.setVisible('strawberry_smoothie',false);

g_form.setVisible('blueberry_smoothie',false);

}

}

 

Should I add an else loop to then switch it back?

Kind regards, 

G

could you please try below code: 


function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || newValue == '') {

return;

}

if (newValue == 'multigrain') {

g_form.setVisible('strawberry_smoothie',false);

g_form.setVisible('blueberry_smoothie',false);

if(newValue == 'white bread') {

g_form.setVisible('strawberry_smoothie',true);

g_form.setVisible('blueberry_smoothie',true);

}

}

Hi @chaudharymahesh 

 

Your comment really helped and I have just implemented an else loop in the script, thank you so much!

 

Kind regards, 

G