Hide/Show a variable

dev_K
Tera Contributor

Hi All,

 

 

In my catalog item I have a list collector, where one of the options is 'other'. I would like to show (and make mandatory) a string field 'other consumer' when this option is selected.

 

Currently I have a client script, but when I select 'other' the field 'other consumer' does not show:

 

 

dev_K_3-1723621187587.png

 

 

 

 

 

Here is the condition:

 

dev_K_2-1723621085009.png

 

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

Hi @dev_K 

use following script, which is working fine:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get the values from the list collector
    var selectedValues = g_form.getValue('consumer_list');
    
    // Check if 'Other' is selected
    if (selectedValues.indexOf('Other') !== -1) {
        // Show and make mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', true);
        g_form.setMandatory('other_consumer', true);
    } else {
        // Hide and make not mandatory the 'other_consumer' field
        g_form.setDisplay('other_consumer', false);
        g_form.setMandatory('other_consumer', false);
    }
}

If you want to check your script, please share here to verify.

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

Thank you

rajesh.

View solution in original post

6 REPLIES 6

var selectedValues = g_form.getValue('consumer');
var selectedArray = selectedValues.split(',');

for (var i = 0; i < selectedArray.length; i++) {
  if (selectedArray[i] === 'Other') {
    g_form.setDisplay('other_consumer', true);
    g_form.setMandatory('other_consumer', true);
    break; // exit the loop if "Other" is found
  } else {
    g_form.setDisplay('other_consumer', false);
    g_form.setMandatory('other_consumer', false);
  }
}

 

 

this does not work:/ I still cant see the hidden field

Hi @dev_K 

Could you please print the var 'selectedArray' once and check value 'Other' is coming correctly there?

Also add logs in 'IF' condition and check condition working correctly or not.

 

thank you