- 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 08:19 PM
Hi Ryan, you have several options:
a) use control
b) use $("element_id")
c) use gel("element_id")
On any of the options your .style.backgroundColor = "red"; should work fine! Perhaps the problem is that you're not using the right element id of the element you want to modify.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2015 08:23 PM
Here goes an example the one I hope it's helpful. Using a variable of catalog script i can modify its background with the following line:
gel('ni.IO:5f72526c4fc20200fbb73fb28110c7e3_label').style.backgroundColor = "red";
where ni.IO:5f72526c4fc20200fbb73fb28110c7e3_label is the id of the element. As you can see for instance, this one finish in _label and the first part of the id is a concatenation between the variable sys_id and the prefix ni.IO: That nomenclature is very specific for catalog variables.
You can always check which is the id that you need by doing an inspect element on your browser.
I hope this is helpful!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2015 11:51 PM
Is the field color required for tables or catalog items ? If it is table, use field styles.
Defining Field Styles - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2015 09:38 PM
Hi Ryan,
Did it worked?
If it did, would you mind closing this thread by marking the responses as helpful/correct, or please let us know if you have any further questions.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2015 12:38 PM