Trying to change background color of single line textfield in ServicePortal and unable to do thT

CharanKulukuri
Tera Contributor

I want to change the background color of single line text field for a catalog item on ServicePortal when user is trying to submit a form.

Basically it should work on an onChange of other field. the field I want to change is just a single line text and Presetted value in it. it will be visible based on the other field.

I tried Isolated script true and false and also tried g_form.setStyle and getControl nothing worked.

1 ACCEPTED SOLUTION

Rakesh_M
Kilo Sage

Hello @CharanKulukuri ,
Modify and add these lines of code as per requirement

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var title=this.document.getElementById("sp_formfield_text");
	if(newValue=="1")
	{
		title.style.backgroundColor="blue";
	}
	else
	{
			title.style.backgroundColor="red";
	}
}

// here Element ID is "sp_formfield_+VariableName". 
//Prefix "sp_formfield_" is  concatenated
//if variable name is subnet_retrieved then element ID is "sp_formfield_subnet_retrieved"


As mentioned by @Ankur Bawiskar DOM manipulation can cause scripts to break or generate unexpected errors especially after platform upgrades.
 

View solution in original post

7 REPLIES 7

Rakesh_M
Kilo Sage

Hello @CharanKulukuri ,
Modify and add these lines of code as per requirement

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var title=this.document.getElementById("sp_formfield_text");
	if(newValue=="1")
	{
		title.style.backgroundColor="blue";
	}
	else
	{
			title.style.backgroundColor="red";
	}
}

// here Element ID is "sp_formfield_+VariableName". 
//Prefix "sp_formfield_" is  concatenated
//if variable name is subnet_retrieved then element ID is "sp_formfield_subnet_retrieved"


As mentioned by @Ankur Bawiskar DOM manipulation can cause scripts to break or generate unexpected errors especially after platform upgrades.
 

Hi @Rakesh_M ,

This kind of worked but the colour changed for field lable but not for value.
Screenshot 2026-04-08 162030.png

 

Is there a way I can change to for field value rather than field name?

 

Rakesh_M
Kilo Sage

Hi @CharanKulukuri ,

Please inspect the page and identify the correct element ID for the input field, then update your script to target that element instead.