Skip to main content
johnnyZ
Inspiring
April 20, 2023
Respondido

Change text color based on field data

  • April 20, 2023
  • 2 respostas
  • 2814 Visualizações

I have a "Read Only" field that, by default says, "This will automatically enter after you complete Page 2".  That text is in a "kinda" rusty brown color.  Once the user types data into fields on page 2, that data is put into this field and I would like it to change to blue.

 

Again, When fields on Page 2 are empty, default text in rusty brown color, when fields on page 2 are filled, text appears in blue.

 

I also need a color chart to find the specific color name.

Here's my Custom Calculation Script:

 

var fn = this.getField("Patients_First_Name").valueAsString;
var mn = this.getField("Patients_Middle_Name").valueAsString;
var ln = this.getField("Patients_Last_Name").valueAsString;

event.target.textColor = color.blue;
if(!fn&&!mn&&!ln)
event.value = "This will automatically enter after you complete Page 2";
else if(fn&&mn&&ln)
event.value = fn+" "+mn+" "+ln;
else if(fn&&!mn&&ln)
event.value = fn+" "+ln;
else if(!fn&&mn&&ln)
event.value = ln;
else if(fn&&mn&&!ln)
event.value = fn;
else if(fn&&!mn&&!ln)
event.value = fn;
else if(!fn&&!mn&&ln)
event.value = ln;
else
event.value = "";

 

Este tópico foi fechado para respostas.
Melhor resposta por Nesa Nurani

The reason it's not working is because the two texts are not identical. This is not a good way of doing it. 

Instead, do it like this:

 

 

if (!fn && !mn && !ln) {
    event.value = "This will automatically fill-in after you complete Page 2";
    event.target.textColor = ["RGB", 175 / 255, 102 / 255, 74 / 255];
} else {
    if (fn && mn && ln)
        event.value = fn + " " + mn + " " + ln;
    else if (fn && !mn && ln)
        event.value = fn + " " + ln;
    else if (!fn && mn && ln)
        event.value = ln;
    else if (fn && mn && !ln)
        event.value = fn;
    else if (fn && !mn && !ln)
        event.value = fn;
    else if (!fn && !mn && ln)
        event.value = ln;
    else
        event.value = "";
	event.target.textColor = color.blue;
}

 

Edit: Error in code fixed

 


There is an error in your script, a 'colon' after you set RGB color.

2 Respostas

try67
Community Expert
Community Expert
April 20, 2023

To change the text color of the field you can use this:

event.target.textColor = color.blue;

Or you could specify an RGB value, like this:

event.target.textColor = ["RGB", 0.22, 0.5, 0];

The values are between 0 and 1, so if you find a RGB value between 0 and 255 (as it's usually presented), divide those values by 255 to get the correct result. You can find such values online.

Nesa Nurani
Community Expert
Community Expert
April 20, 2023

Remove line with color.blue and put this at the bottom of your script:

event.target.textColor = event.value == "This will automatically enter after you complete Page 2" ? ["RGB", 175/255, 102/255, 74/255] : color.blue;

Built in colors are limited to basic colors so use RGB for any color you want like I did: RGB 176,102,74 is a shade of rusty brown, but you can change it to any color you want.

johnnyZ
johnnyZAutor
Inspiring
April 20, 2023

Thanks Nesa,

It works "initially".  I set the default "Starting" color in Properties-Appearance-Text Color.  Once I enter the text on Page 2, the text changes and it turns blue,  but then, if I delete the data from the fields on Page 2, the text changes back but stays blue.  It doesn't change back to the "Starting" default color.

Here's my script now:

var fn = this.getField("Patients_First_Name").valueAsString;
var mn = this.getField("Patients_Middle_Name").valueAsString;
var ln = this.getField("Patients_Last_Name").valueAsString;

if(!fn&&!mn&&!ln)
event.value = "This will automatically fill-in after you complete Page 2";
else if(fn&&mn&&ln)
event.value = fn+" "+mn+" "+ln;
else if(fn&&!mn&&ln)
event.value = fn+" "+ln;
else if(!fn&&mn&&ln)
event.value = ln;
else if(fn&&mn&&!ln)
event.value = fn;
else if(fn&&!mn&&!ln)
event.value = fn;
else if(!fn&&!mn&&ln)
event.value = ln;
else
event.value = "";
event.target.textColor = event.value == "This will automatically enter after you complete Page 2" ? ["RGB", 175/255, 102/255, 74/255] : color.blue;
try67
Community Expert
Community Expert
April 20, 2023

The reason it's not working is because the two texts are not identical. This is not a good way of doing it. 

Instead, do it like this:

 

 

if (!fn && !mn && !ln) {
    event.value = "This will automatically fill-in after you complete Page 2";
    event.target.textColor = ["RGB", 175 / 255, 102 / 255, 74 / 255];
} else {
    if (fn && mn && ln)
        event.value = fn + " " + mn + " " + ln;
    else if (fn && !mn && ln)
        event.value = fn + " " + ln;
    else if (!fn && mn && ln)
        event.value = ln;
    else if (fn && mn && !ln)
        event.value = fn;
    else if (fn && !mn && !ln)
        event.value = fn;
    else if (!fn && !mn && ln)
        event.value = ln;
    else
        event.value = "";
	event.target.textColor = color.blue;
}

 

Edit: Error in code fixed