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

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

Community Beginner ,
May 23, 2017 May 23, 2017

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?

TOPICS
PDF forms
16.0K
Translate
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
2 ACCEPTED SOLUTIONS
Community Expert ,
May 24, 2017 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.

View solution in original post

Translate
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
Explorer ,
Mar 12, 2025 Mar 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. 

View solution in original post

Translate
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 ,
May 24, 2017 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.

Translate
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 Beginner ,
May 24, 2017 May 24, 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;

}

Translate
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
Explorer ,
Oct 24, 2019 Oct 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
}

Translate
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
New Here ,
May 21, 2020 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 

Translate
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
LEGEND ,
May 21, 2020 May 21, 2020

You should be able to take hollyj's reply as a model. Be sure you understand the importance of the numbers in the RGB values. Best to read the question and compare to see what was needed.

Translate
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
New Here ,
May 21, 2020 May 21, 2020

i put this in the JavaScript edior and saved the document byit is just showed green whatever figure I put in?

 

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

if (f>=400.00 && f<=2000.00)

event.target.textColor = color.red;

else if (f>=0.00 && f<=400.00)

event.target.textColor = color.green;

}

Translate
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
LEGEND ,
May 21, 2020 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)?

Translate
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
New Here ,
May 21, 2020 May 21, 2020

I have not seen any meassages

Its in the validate tab 

Translate
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
New Here ,
May 21, 2020 May 21, 2020

Should this be in a different script editor?

Translate
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 ,
May 21, 2020 May 21, 2020

You'll find a tutorial on the Console Window here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
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
New Here ,
May 22, 2020 May 22, 2020

Hi all sorted thank you, it wa snot in the correct field now works very well

i also need to create a text box that states PASS or FAIL if all three boxes FD1 FD2 and FD3 have not rurned Green (<400) but i dont now how to vreate the text based on the result of the three results.

could you help?

Translate
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
New Here ,
Jun 11, 2020 Jun 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;

Translate
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 ,
Jun 11, 2020 Jun 11, 2020

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

Translate
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
LEGEND ,
Jun 12, 2020 Jun 12, 2020

While it's an oversimplification, it's best to say "Use == only for strings and numbers". color.white is neither of these, in fact we don't know exactly what it is and shouldn't try to find out. Hence, the provision of a special method to check equality is very useful to us.

Translate
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
New Here ,
Jun 12, 2020 Jun 12, 2020

Thank you both for answer, now i have a solution.

getField("fieldName").fillColor = [ "G", 1 ];
if (color.equal([ "G", 1 ], this.getField("fieldName").fillColor))
getField("fieldName").display = display.hidden;

Translate
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 Beginner ,
Jan 21, 2021 Jan 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!!

Translate
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 ,
Jan 21, 2021 Jan 21, 2021

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

Translate
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 Beginner ,
Jan 21, 2021 Jan 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

Translate
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 ,
Jan 21, 2021 Jan 21, 2021

E.g.:

if (event.value == 5) {
  this.getField("field name").textColor = color.red;
}

Translate
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 Beginner ,
Jan 21, 2021 Jan 21, 2021

super - danke!

Translate
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
Explorer ,
Jun 20, 2024 Jun 20, 2024

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

 

What happens if you take the first part of my script? 

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

Translate
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
Explorer ,
Jun 20, 2024 Jun 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?

Translate
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
Explorer ,
Mar 12, 2025 Mar 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. 

Translate
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
Adobe Employee ,
Apr 24, 2025 Apr 24, 2025
LATEST

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.

Translate
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