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
Braniac
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
Braniac
June 11, 2020

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

try67
Braniac
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.

Participating Frequently
May 21, 2020

Hi

 

I am new to Acrobat Form creation and would like to change a text colour based on its value

Green if under 400 and red if above 400

i would be grateful if you could help the text box is labelled as FD1 on teh form

 

Thank you 

Participating Frequently
May 21, 2020

Looks ok, at first glance.

1. Do you get any messages in the JavaScript console when you change the value?

2. Where do you add this JavaScript (there are so many places you might choose)?


I have not seen any meassages

Its in the validate tab