Skip to main content
Inspiring
May 24, 2017
Answered

Javascript - Change fill colour depending on the value in the field

  • May 24, 2017
  • 4 replies
  • 16229 views

I have been using the forums to help me answer the question above and I have managed to create the code below.

var f = this.getField("Text552").value;  {

if (f>=1 && f<=5)

event.target.fillColor = ["RGB",153,204,0]; 

else if (f>=6 && f<=8) 

event.target.fillColor =["RGB",255,204,153]; 

else if (f>=9 && f<=15) 

event.target.fillColor = ["RGB",255,139,61];

else if (f>=16 && f<=25) 

event.target.fillColor = ["RGB",214,0,0];

else event.target.fillColor = color.white;

}

Using this code the fill colour changes for values 1-5 and 16-25 but not for the rest. Can anyone suggest what I might be doing wrong?

Correct answer Sharon38055632khef

I found it, it was as simple as the box was checked red instead of none. It works fine now and thank you for all your help today. 

4 replies

Inspiring
June 20, 2024

I have a script where I am having two issues.  What I want the script to do is turn any number over 5 red and any number more than -5 red. I need the number to return to transparent if a number is changed back within the transparent parameters. 

My script is:

if(event.value >= 5)
event.target.fillColor= color.red;
if(event.value >= -5)
event.target.fillColor= color.transparent;

 

I have tried to figure out the - 5 and also to have the number return to transparent when parameters change but can't seem to get there. Any help?

Sharon38055632khefCorrect answer
Inspiring
March 12, 2025

I found it, it was as simple as the box was checked red instead of none. It works fine now and thank you for all your help today. 

S_S
Community Manager
Community Manager
April 24, 2025

Hi @Sharon38055632khef,

 

Hope you are doing well. 

 

Thanks for sharing about what worked for you.

Marking this as a correct answer for future users to use as a reference.


Regards,
Souvik.

New Participant
January 21, 2021

Hello i need Help please.

Everytime i try this script it says:
TypeError: event.target is undefined
19:Console:Exec
undefined

 

I just want to change the Text-Colour to red if i type the Number "5" in a certain Textfield in Acrobat.
There are lots of Texfields and i need the Script for all of them (not only for one specific Field).

I dont know what to do, tried a lot of different scripts but none of them worked yet. Would be great if someone could solve my Problem. Thank you!!

Bernd Alheit
Community Expert
January 21, 2021

Use a script at validation of the textfield. You can't use event.target in the console.

New Participant
January 21, 2021

Hallo Benrd,
leider bin ich mit dem Thema Scripten usw nur sehr wenig vertraut bzw. kenne mich leider nicht gut aus. Hast du vielleicht eine Idee wie ich das machen kann?

Danke und LG

liborb94033412
New Participant
June 11, 2020

Why this does not work? The field is still visible.

getField("fieldName").fillColor = color.white;

if (getField("fieldName").fillColor == color.white) // here is some problem
getField("fieldName").display = display.hidden;

Bernd Alheit
Community Expert
June 11, 2020

For comparing colors, use the color.equal() function.

try67
Community Expert
May 24, 2017

It shouldn't work for any of them, except for the last one. The reason is you're using the wrong numeric values.

The color object uses values from 0 to 1, not 0 to 255. Divide all your values by 255 and it should work correctly.

Inspiring
May 25, 2017

Thank you. With your help and some more research, I now have the below code and it works perfectly.

var f = this.getField("Text552").value;  {

if (f>=1 && f<=5)

    event.target.fillColor =["RGB",0.537,0.776,0]; 

else if (f>=6 && f<=8) 

    event.target.fillColor =["RGB",1,0.804,0.623]; 

else if (f>=9 && f<=15) 

    event.target.fillColor = ["RGB",1,0.709,0];

else if (f>=16 && f<=25) 

    event.target.fillColor = ["RGB",1,0,0];

else event.target.fillColor = color.white;

}

kandices70514105
Known Participant
October 24, 2019

I think I amhaving trouble because I don't understand the terms.  I need the background color on my text form field to change when text is entered in to the box.  for example when i enter my initials i want it to change color to separate the complete items from the incomplete items on my list. The discussion above is so far over my head I'm lost.  I have figured some of these things out before but can't mange to get this one to work.  This is what I have...

 

if(this.3rd Qtr.0 != null)
{
this.border.fill.color.value >" "; // colour light blue
}
else
{
this.border.fill.color.value = null; // colour white
}