Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

I want to implement onChange client script for changing the color of additional comment field based on checked and unchecked of checkbox. Please provide me the solution

dhanu3
Kilo Sage

I want to implement onChange client script for changing the color of additional comment field based on selection of checkbox. Please provide me the solution..

1 ACCEPTED SOLUTION

dhanu3
Kilo Sage

Hi,

Thank you for your help.

Now it is working with below code.

 

var c = document.getElementById("activity-stream-comments-textarea");

if (newValue == 'true') {
c.style.backgroundColor = '#FFFFE0';
}
else {
c.style.backgroundColor = '';
}

 

Thanks,

Dhanashree

View solution in original post

6 REPLIES 6

jtsmp
Giga Contributor

You need to use DOM manipulation.  t is on the textarea itself setting the background-color:

Laurie Marlowe1
Kilo Sage

Hi Dhanu,

You can do this using Styles.  There are conditions you can apply to a style (if your checkbox is checked, show one background color, if not, show a different background color).

https://docs.servicenow.com/bundle/kingston-platform-administration/page/administer/navigation-and-u...

Thanks,

Laurie

 

Please mark correct or helpful as applicable!

 

Hi Laurie,

 

I am trying to implement it but not working. Don't know why.

find_real_file.png

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

find_real_file.png

Please guide me.

 

Thanks,

Dhanashree

Hi Dhanashree,

My fault.  I gave you bad advice.  The condition (Value field) only works on list view.  If you leave the Value field blank, then the format in the Style file will apply to both list view and the field in form view.

You will have to use a client script as you originally said.  Here is a client script that sets the background color of the incident caller field to blue, if the caller is a VIP.  NOTE:  this code will NOT work on a mobile platform as it contains DOM manipulation. 

function onChange(control, oldValue, newValue, isLoading) {
	
	var callerField = $('sys_display.incident.caller_id');
	if (!callerField)
		return;
	
	if (!newValue) {
		
		callerField.setStyle({color: ""});
		return;
	}

	g_form.getReference('caller_id', vipCallerCallback);
}

function vipCallerCallback(caller) {
	
	var callerField = $('sys_display.incident.caller_id');
	if (!callerField)
		return;
	
	//check for VIP
	
		
	if (caller.vip == 'true') {
		if (document.documentElement.getAttribute('data-doctype') == 'true')
		
		callerField.setStyle({background-color: "blue"});
	}
	
	
	else {
		
		callerField.setStyle({color: ""});
	}
	
	
}

You may have to tweak this a little.  Hope it helps.

 

Thanks,

Lauire

Please mark correct or helpful as applicable!