Skip to main content
Participant
February 26, 2025
Answered

How to create a form field that turns different colors depending on its value?

  • February 26, 2025
  • 2 replies
  • 684 views

For example:

 

If the value of the field is between 8-10: turn the field red.

If the value of the field is between 6-10: turn the field orange.

If the value of the field is between 4-6: turn the field yellow.

If the value of the field is between 0-4: turn the field green.

 

Also I would like to be able to change the fill of the field as well as the color of the text.

(for example a red fill with white text, but a yellow fill with black text) so that everything is readable.

Correct answer Nesa Nurani

You are missing event.value before <= 0.35

2 replies

try67
Community Expert
Community Expert
February 27, 2025

Just be aware that not all colors have such an easy "shortcut". For other colors you will need to specify their RGB/CMYK codes manually. For example, there's no color.orange, but you can use this:

 

event.target.fillColor = ["RGB", 255/255, 165/255, 0/255];

 

Notice the values are between 0 and 1, not 0 and 255, hence the division by 255.

Participant
February 27, 2025

This code is producing SyntaxError: invalid XML name.

 

if (event.value > 0 && event.value <= 0.25){
event.target.fillColor = color.green;
} else if (event.value >= 0.26 && <= 0.35){
event.target.fillColor = color.red;
}

 

Are you able to help?

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 27, 2025

You are missing event.value before <= 0.35

Thom Parker
Community Expert
Community Expert
February 26, 2025

Use the Custom Validation script on each field.  

 

For example: 

    if(event.value <=4 )

         event.target.fillColor = color.green;

 

Here's an article on how to edit field properties such as the Validation Script.  

https://www.pdfscripting.com/public/Editing-Fields-Properties.cfm

 

Here's the reference entry for form field properties:

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

 

You'll find more information on scripting form fields here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

You're value ranges are unworkable, so you'll need to modify them. 

 

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