Skip to main content
Inspiring
May 31, 2017
Answered

Javascript on a pdf form to change the colour of a field depending on the value in another field

  • May 31, 2017
  • 4 replies
  • 15634 views

I have used the following code to change the colour of a text field in a PDF form depending on the value entered into that field.

var v = +event.value; {

if (v>=1 && v<=5) 

event.target.fillColor = ["RGB",0.537,0.776,0]; 

Can anyone suggest how I can modify this code to change the colour of another field?

I want to change the colour of field "Text453" depending on the number entered into field "RISK"

(I have posted this question on another forum.)

This topic has been closed for replies.
Correct answer try67

Thanks for the tips. I managed to work it out and went with the code below.

var v = +event.value;

{if (v == 1)  

this.getField("Text803").fillColor = ["RGB",0.537,0.776,0];

else this.getField("Text803").fillColor = color.white;}

var v = +event.value;

{if (v == 1)

this.getField("Text804").fillColor = ["RGB",0.537,0.776,0];

else this.getField("Text804").fillColor = color.white;} 

var v = +event.value;

{if (v == 2)

this.getField("Text805").fillColor = ["RGB",0.537,0.776,0];

else this.getField("Text805").fillColor = color.white;} 

var v = +event.value;

{if (v == 2)

this.getField("Text806").fillColor = ["RGB",0.537,0.776,0];

else this.getField("Text806").fillColor = color.white;}

var v = +event.value;

{if (v == 3)

this.getField("Text807").fillColor = ["RGB",0.537,0.776,0];

else this.getField("Text807").fillColor = color.white;} 


The correct syntax is to put the curly brackets after the if-condition. It works in your case because you only have one line of code associated with it, but it's not good practice. This is how it should look like:

var v = +event.value;

if (v == 1) {

    this.getField("Text803").fillColor = ["RGB",0.537,0.776,0];

} else {

    this.getField("Text803").fillColor = color.white;

}

4 replies

Legend
May 27, 2019

You said you wanted to use a light green and a light pink.

We asked if you had the RGB colours to use, and you said yes.

I asked to see the code using the colours, which had problems.

I am still trying to answer your first question.

ihteshamu35126935
Participating Frequently
May 27, 2019

sir  our requirements  changes no RBG needed. i want  you help me.

How can i change my combobox/dropdown list background color change.

i have  YES    &   No in my dropdown list

i want YES in green back ground

& no in RED backround.

i want to know how.

Consider me as bigneer.

try67
Community Expert
Community Expert
May 27, 2019

That's not possible. You can't assign different colors to various options

in the field.

On Mon, May 27, 2019, 23:02 ihteshamu35126935 <forums_noreply@adobe.com>

Legend
May 27, 2019

Ok, please post your attempted code with the RGB colours. We cannot guess what problem you are having. And please answer ALL THE QUESTIONS. Where did you place the code?

ihteshamu35126935
Participating Frequently
May 27, 2019

i place below codes in combobox>>> properties>>>>vaildate

& its no working sir

all i want is combobox background colors for different selections i.e YES in green background & NO in RED background.

var f_color = color.white;

if (event.value === "NO") f_color = color.red;

if (event.value === "Manager NO") f_color = color.red;

if (event.value === "YES") f_color =color.green;

event.target.fillColor = f_color; 

Legend
May 26, 2019

You did not answer the question. Do you know the RGB values for these colours? Please answer. Do not ignore any part of the reply.

ihteshamu35126935
Participating Frequently
May 27, 2019

YES sir i know RGB colors

try67
Community Expert
Community Expert
May 31, 2017

Change:

event.target.fillColor = ["RGB",0.537,0.776,0];

To:

this.getField("Text453").fillColor = ["RGB",0.537,0.776,0];

ihteshamu35126935
Participating Frequently
May 26, 2019

i need light pink and light green can you help

var f = event.target;  
var fv = event.value;

if (fv == "Good") {
  f
.fillColor = color.green;
}
else if (fv == "Fair") {
  f
.fillColor = color.yellow;
}
else if (fv == "Poor") {
  f
.fillColor = color.red;
}
else {
  f
.fillColor = color.white;
}

ihteshamu35126935
Participating Frequently
May 26, 2019

You did not answer my question, nor specified what you tried and what the results were. I can't help you if you don't provide basic information.


sir i need color drop down list in PDF in color back ground

YES  as light green color.

NO   as light pink

Manager no as RED color.

var f = event.target;  
var fv = event.value;

if (fv == "YES") {
  f
.fillColor = color.green;
}
else if (fv == "NO") {
  f
.fillColor = color.yellow;
}
else if (fv == "Manager NO") {
  f
.fillColor = color.red;
}
else {
  f
.fillColor = color.white;
}