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

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

Views

12.6K

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

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.

Votes

Translate

Translate
Community Expert ,
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.

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

}

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
Explorer ,
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
}

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

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
LEGEND ,
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.

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

}

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

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

Copy link to clipboard

Copied

I have not seen any meassages

Its in the validate tab 

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

Copy link to clipboard

Copied

Should this be in a different script editor?

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

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

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

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

Copy link to clipboard

Copied

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

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
LEGEND ,
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.

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

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

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

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

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

Copy link to clipboard

Copied

E.g.:

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

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

Copy link to clipboard

Copied

LATEST

super - danke!

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