Dynamic field color help

Ryan Norton
Giga Contributor

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;

  }

}

1 ACCEPTED SOLUTION

Ryan Norton
Giga Contributor

I'd like to thank everyone for being so helpful, your posts inspired my route for doing this.


Here's the script I used:



find_real_file.png


View solution in original post

9 REPLIES 9

Ryan Norton
Giga Contributor

this script was found on service now's wiki


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.


The SN Nerd
Giga Sage
Giga Sage

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

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.