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

Custom formatting for drop down menu color change.

New Here ,
Nov 08, 2022 Nov 08, 2022

Hi, 

Being a beginner at Javascript and have been looking and searching for the right code. 

I really hope that you can help me.

 

I have created a fillable PDF with a dropdown menus. The dropdowns have 3 options and want each option to result in a color.

This is the script I currently have been working on i'm able to get the color to change for "Complete" but not for "In Progress" or "Not Started"

 

if (event.value=="Complete")

event.target.fillColor = color.green;

else if (event.value=="In_Progress")

event.target.fillColor = color.yellow;

else if (event.value=="Not­_Started")

event.target.fillColor = color.Red;

event.target.fillColor = color.transparent;

 

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms , Standards and accessibility
2.2K
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 ,
Nov 08, 2022 Nov 08, 2022

-It's "color.red", not "color.Red".

-"color.yellow" is a CMYK color, not an RGB one. Try it with an RGB code, instead.

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 ,
Nov 08, 2022 Nov 08, 2022

I changed it to "color.red" still did not work 

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 ,
Nov 08, 2022 Nov 08, 2022

The last line overrides the others. Looks like you are missing an "else"

And the "Commit value immediately" option should be checked. 

 

 

if (event.value=="Complete")
   event.target.fillColor = color.green;
else if (event.value=="In_Progress")
   event.target.fillColor = color.yellow;
else if (event.value=="Not­_Started")
   event.target.fillColor = color.Red;
else
   event.target.fillColor = color.transparent;

 

 

 

 

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
Community Expert ,
Nov 08, 2022 Nov 08, 2022

++Adding to the topic,

 

This also worked for me as a Custom Format Script.

 

if (event.value == "Complete") event.target.fillColor = color.green;

else { 

if (event.value == "In Progress")event.target.fillColor = color.yellow;

else {

if (event.value = "Not­ Started")event.target.fillColor = color.red;

  }
 }

 

I am not sure what condition will trigger the transparent field color property.  Is there a blank space that allows the user to select from the dropdown menu?

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 ,
Nov 08, 2022 Nov 08, 2022

Yes, there is a blank space. That script seemed to work best but yellow is not selcted for "In Progress" it comes up as red. Any thoughts?

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 ,
Nov 08, 2022 Nov 08, 2022

++ EDITED REPLY,  added screenshot

 

 

Well, what worked for me with no issues is the example below as a Custom Format Script:

 

 

 

 

if (event.value =="" || event.value == " ") event.target.fillColor = color.transparent;

else {

if (event.value == "Complete") event.target.fillColor = color.green;

else { 

if (event.value == "In_Progress")event.target.fillColor = color.yellow;

else {

if (event.value = "Not­_Started")event.target.fillColor = color.red;

  }
 }
}

 

 

 

 

NOTE: It worked for me too as a custom calculation script

 

As mentioned by Thom Parker, in the Dropdown Properties dialogue window, go to the "Options" tab, and you must tick the checkbox "Commit selected values immediately" for the the script to commit the field color properties as soon as a selection is made in the dropdown; otherwise you'll experience a delay or will not work at all.

 

See slide below:

 

options.pngexpand image

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 ,
Nov 09, 2022 Nov 09, 2022

Is it "In Progress" or "In_Progress"? You have to make sure the string in your code matches the actual value PERFECTLY, including upper/lower-case letters, spaces, etc.

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 ,
Nov 09, 2022 Nov 09, 2022
LATEST

Than you for noting that Try67.

 

In my example  slide I was testing the script with a space instead of the underscore character for those listed items.

 

 

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