Onchange client script to set value on hidden variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 01:57 AM
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 03:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 04:00 AM - edited 12-07-2023 04:01 AM
I guess this solution is already given by me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 04:12 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 04:38 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2023 04:59 AM
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