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

Community Beginner ,
May 23, 2017 May 23, 2017

Copy link to clipboard

Copied

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, Windows

Views

6.0K

Likes

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 1 Correct Answer

Most Valuable Participant , 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.

Likes

Translate

Translate
Most Valuable Participant ,
May 24, 2017 May 24, 2017

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

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;

}

Likes

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 Beginner ,
Oct 24, 2019 Oct 24, 2019

Copy link to clipboard

Copied

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
}

Likes

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

Copy link to clipboard

Copied

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 

Likes

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
Most Valuable Participant ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

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;

}

Likes

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
Most Valuable Participant ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

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)?

Likes

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

Copy link to clipboard

Copied

I have not seen any meassages

Its in the validate tab 

Likes

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

Copy link to clipboard

Copied

Should this be in a different script editor?

Likes

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
Adobe Community Professional ,
May 21, 2020 May 21, 2020

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

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?

Likes

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

Copy link to clipboard

Copied

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;

Likes

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
Adobe Community Professional ,
Jun 11, 2020 Jun 11, 2020

Copy link to clipboard

Copied

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

Likes

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
Most Valuable Participant ,
Jun 12, 2020 Jun 12, 2020

Copy link to clipboard

Copied

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.

Likes

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

Copy link to clipboard

Copied

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;

Likes

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

Copy link to clipboard

Copied

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!!

Likes

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
Adobe Community Professional ,
Jan 21, 2021 Jan 21, 2021

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

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

Likes

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
Adobe Community Professional ,
Jan 21, 2021 Jan 21, 2021

Copy link to clipboard

Copied

E.g.:

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

Likes

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

Copy link to clipboard

Copied

LATEST

super - danke!

Likes

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