The CreatorCon Call for Content is officially open! Get started here.

I want on Change Client Script for field background color

LathifabegaS
Kilo Contributor
Each time the state field changes, number fields background colour should be changed
8 REPLIES 8

@LathifabegaS , Have you tried the solution I have provided, If yes then where are you stuck. You can add more complex logic in the script I have given. Please let me know if I can help with that.

 

@LathifabegaS , If this solution helped you Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.

Hi @LathifabegaS , Have you tried wriing the script with the logic and demo script I have given above? Did it help? Where are you stuck now?

 

If my answers given earlier were helpful then Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.

Bert_c1
Kilo Patron

I use the following to conditionally set the background color in an olLoad client script for the sys_user table:

 

function onLoad() {
  //Set background color for VIP users.
    var element = g_form.getElement('user_name');
    var vipUser = g_form.getValue('vip');
    if (vipUser == 'true') {
        element.style.backgroundColor = "red";
    }
}

Same logic can be used on an onChange client script.

Ravi Gaurav
Giga Sage
Giga Sage

Hi @LathifabegaS 

use the below CS on Change 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}

var fieldName = 'priority'; // field you're targeting
var ctrl = g_form.getControl(fieldName);

if (ctrl) {
// Reset style first (important if user changes values multiple times)
ctrl.style.fontWeight = 'normal';
ctrl.style.backgroundColor = '';

if (newValue == '4') {
ctrl.style.fontWeight = 'bold';
ctrl.style.backgroundColor = 'lightblue';
} else if (newValue == '1') {
ctrl.style.fontWeight = 'bold';
ctrl.style.backgroundColor = 'red';
}
}
}

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/