• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change text color based on field data

Participant ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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 = "";

 

TOPICS
Create PDFs , JavaScript , PDF forms

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 20, 2023 Apr 20, 2023

You changed message:

"This will automatically fill-in after you complete Page 2"

"This will automatically enter after you complete Page 2"

Fix that and it will work.

 

Votes

Translate

Translate
Community Expert , Apr 20, 2023 Apr 20, 2023

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

Votes

Translate

Translate
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

You're right. I copied it over and didn't adjust it properly. It's fixed now.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

You changed message:

"This will automatically fill-in after you complete Page 2"

"This will automatically enter after you complete Page 2"

Fix that and it will work.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

LATEST

Nesa,

You're the BEST !!!  It works great now.

Thank you so much.

Yes, I wasn't sure exactly how to word that message.  "fill-in" or "enter".

You are VERY observant.

I'm still playing around with it and deciding.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2023 Apr 20, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines