Skip to main content
Known Participant
December 6, 2022
Answered

Change fill.Color to yellow only if previous color is green

  • December 6, 2022
  • 1 reply
  • 555 views

This seems somthing that should be easy, but I cannot get it to work, or maybe it just cannot be done!

Bascially, I have a field which background can be red, yellow or green.

I have a checkbox, that will make the color change. I want to change the color to yellow, only if the field is green. If the field is red, it will stay red.

if ((this.getField("AS").value == "1") && (this.getField("Exercise").fillColor == color.green))
{this.getField("Exercise").fillColor = color.yellow;}

This code doesn't change to color of the field.

When I try fillColor = color.green; the color changes to yellow, even if it's red.

This topic has been closed for replies.
Correct answer Thom Parker

Colors can't be compared with the "==" symbol. Colors are more complex than  this. Fortunately there is a color compare function in the SDK. 

Here's the reference entry:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#equal

 

Here's the updated code

if ((this.getField("AS").value == "1") && color.equal(this.getField("Exercise").fillColor, color.green))
{this.getField("Exercise").fillColor = color.yellow;}

 

There may be other things that need addressing. For example, is the name of the checkbox field "AS"? Is this code in the MouseUp event of the checkbox? (If so the code should be written differently). Are there other scripts that are competing to set the color of the field? Where are those scripts? what events are triggering them?  

 

 

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 6, 2022

Colors can't be compared with the "==" symbol. Colors are more complex than  this. Fortunately there is a color compare function in the SDK. 

Here's the reference entry:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#equal

 

Here's the updated code

if ((this.getField("AS").value == "1") && color.equal(this.getField("Exercise").fillColor, color.green))
{this.getField("Exercise").fillColor = color.yellow;}

 

There may be other things that need addressing. For example, is the name of the checkbox field "AS"? Is this code in the MouseUp event of the checkbox? (If so the code should be written differently). Are there other scripts that are competing to set the color of the field? Where are those scripts? what events are triggering them?  

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
yeungyamAuthor
Known Participant
December 7, 2022

Thank you so much Thom.

The code works wonderfully. The script is placed in the calculation tab of a text box, so no need to change the code.

All the other scripts are already taken care of.