Skip to main content
New Participant
August 18, 2023
Answered

Writing a script in a PDF form so colour of text changes based on a value.

  • August 18, 2023
  • 1 reply
  • 435 views

I have a simple script but not sure what is wrong. Basically if the number input to the form is less than, for example, 5. Then turn the font colour red. It works but.. not if you input 0.

 

var A = Number(this.getField("Quantity").valueAsString);

if(A){
 if(A<5)
 event.target.textColor = color.red;
 else
 event.target.textColor = color.black;}

 

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

The first if statement blocks the 0 input, because a value of 0 converts to False.

Remove it. Here's an update.

var A = Number(this.getField("Quantity").valueAsString);

if(A<5)
   event.target.textColor = color.red;
else
   event.target.textColor = color.black;

 

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
August 18, 2023

The first if statement blocks the 0 input, because a value of 0 converts to False.

Remove it. Here's an update.

var A = Number(this.getField("Quantity").valueAsString);

if(A<5)
   event.target.textColor = color.red;
else
   event.target.textColor = color.black;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
GlenPennyAuthor
New Participant
August 19, 2023

This has solved the issue. Much appreciated!