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

bernyalvarado
Mega Sage

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


bernyalvarado
Mega Sage

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!



id.jpg



Kalaiarasan Pus
Giga Sage

Is the field color required for tables or catalog items ? If it is table, use field styles.



Defining Field Styles - ServiceNow Wiki


bernyalvarado
Mega Sage

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


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