- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 02:38 PM
so, I'm trying to write a client script to change the background color of a Field based on its value, but i can't seem to get it to work.
should i use getelement() or getcontrol()
the script im using is
function onChange(control, oldValue, newValue, isLoading) {
var elementID = gel("quantity");
switch (newValue) {
case "1":
elementID.style.backgroundColor = "red";
break;
case "2":
elementID.style.backgroundColor = "tomato";
break;
case "3":
elementID.style.backgroundColor = "orange";
break;
case "4":
elementID.style.backgroundColor = "yellow";
break;
case "5":
elementID.style.backgroundColor = "green";
break;
default:
elementID.style.backgroundColor = "white";
break;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2015 12:38 PM
I'd like to thank everyone for being so helpful, your posts inspired my route for doing this.
Here's the script I used:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 02:38 PM
this script was found on service now's wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 04:19 PM
Hi Ryan,
Gel is basically a replacement of document.getElementById and in your case the real id could be different than quantity. You might want to verify that. Alternatively, you can use control.style.backgroundColor and see if that works for you. control is already referring to the dom object that has got changed so that might be a good way to change the background color.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 07:43 PM
Have you considered using styles?
Right click on the field and select 'Configure Styles'.
For example
Value: red
Style: background-color:Red
This won't work for catalog items but will for any given GlideRecord.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2015 12:31 PM
Hey Paul, sadly because its in a form, styles aren't applicable. they only work in lists of records or if the style has no value set.