Onchange client script to set value on hidden variable

CE_
Tera Contributor

Hi,

I have a hidden field in a form that I need to set value on. Is this possible with on change script?
If variable A has answer "no" then variable B is not visible. But variable B should then be set to "abc". 
So can I use on change client script to set value on this hidden variable? I tried but got "Javascript error in your browser console".

Thanks,

9 REPLIES 9

Shamma Negi
Kilo Sage
Kilo Sage

Dont hide the field on Load rather hide the field from same onchange client script by wriitng below script. In the same onchange script check and hide

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var one = g_form.getValue('variable_1');
    if (one == 'No') {
        g_form.setValue('variable_2''Sample set value ');   //First set the value you want
        g_form.setDisplay('variable_2'false);  // then hide the field

 

    } else {
        g_form.setDisplay('variable_2'true);
    }
    //Type appropriate comment here, and begin script below

 

}
Regards,Shamma Negi

@Shamma Negi 

I guess this solution is already given by me.

Please check if else condition. The original post says that If variable A has answer "no" then variable B is not visible. But variable B should then be set to "abc". So I modified the script accordingly.

 

 

Regards,Shamma Negi

Prabhjot Kaur4
Tera Contributor

Hi,

 

Write onchange Client Script on 'variable A' not on hidden 'variable B'.

When 'variable A' gets change then set the 'variable B' value.

 

Thanks

Prabhjot

Rajesh_Bhise
Tera Guru

Hello @CE_ ,

 

As per the ServiceNow best practice, please make the hidden field visible, set it's value and then hide it.

this is similar to make field non-mandatory and hide it on the form.

You can use the script given by @Prashant Ahire and fulfill your requirement.

Hope this helps your query and understand things behind.

 

Kindly mark my answer has helpful‌‌ and solution proposed‌‌ if this helps you.

Happy to help you in future.😍

 

Thank You,
Rajesh