Clear a field value when non blank value in other field changes

Ajay37
Tera Contributor

Hi All,

 

I have two fields on the catalog form. One is of type choice and other is reference field. Now I need to clear values in each other field when a value in other field changes. But this should happen only when there is a value in a field and it changed to some other value. It shouldn't happen when blank value changed to some other value.

For suppose, on load of the form both choice field and reference field are empty.

Now when user selects a choice in choice field and he moves to fill a value in reference field. Initially reference field is empty on load and when user tries to fill in value, logically the value got changed and script is clearing the value in choice field. But this shouldn't happen in my case, the values should be cleared only when a non blank value got changed to some other non blank value. Please let me know if you need more information.

 

Thanks in Advance

9 REPLIES 9

Tony Chatfield1
Kilo Patron

Hi, unfortunately your post/description is not very clear and with no details of your existing code it is not possible to evaluate your issue.
OOB onChange() catalog Client Scripts do not run if the newValue is '' and so you might want something like this

 

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

//the newValue is not null so clear the other field(s) 
g_form.setValue('someField', '');

}

 If this is not correct, then please update this thread with clear details of your requirements - it might be easier if you called the fields field1, field2 etc,

Hi @Tony Chatfield1 ,

Thanks for the reply.

 

I have two fields on form.

Field 1 - Choice type - Choices(None, A , B) - which is blank when the form loads.

Field 2 - Reference - which is blank when form loads.

 

Now, when Field 1 value changes, field 2 value should get cleared. And when Field 2 value changes, field 1 value should get cleared.

They both are blank initially during loading of the form.

Scenario 1:

  • I set value in Field 1 from None to A. And then I tried to set value in field 2 from blank to X.
  • Field 2 is initially blank and as I tried to set the value in field, logically the value got changed from blank to X, hence On change client script triggers.
  • As the Field 2 value got changed and new value in Field 2 is not empty, according to the client script logic it clears the value in field 1.
  • But this should not happen. I want client script to trigger only when non-empty field value changes like X to Y, Y to Z, Z to X etc. The client script shouldn't get triggered when value changes from blank to X or Y or Z etc.

Scenario 2:

  • I set value in Field 2 from blank to X. And then I tried to set value in field 1 from None to A.
  • Field 1 is initially blank and as I tried to set the value in field, logically the value got changed from None to A, hence On change client script triggers.
  • As the Field 1 value got changed and new value in Field 1 is not empty, according to the client script logic it clears the value in field 2.
  • But this should not happen. I want client script to trigger only when non-empty field value changes like A to B or B to A. The client script shouldn't get triggered when value changes from None to A or B.
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    g_form.clearValue('environments');
    g_form.clearValue('server');

}

 

Hope I have provided the information that required.

 

Thank you.

Hi, not  that I have understood but it sounds like you need to check the oldValue before clearing the target field(s) so maybe something like

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

  if(oldValue != '') {
      g_form.clearValue('environments');
      g_form.clearValue('server');
   }
}

 

Hi Tony,

 

But the if condition (oldValue != '') never evaluates to true because the old value for the fields always return blank. Because there is no default value for them and they will always be blank during the form load.

 

Thanks