Skip to main content
postseventeen
Participant
July 14, 2019
Answered

Dropdown box selection to change background color of text field

  • July 14, 2019
  • 3 replies
  • 8213 views

Hello,  I wonder if anyone could please point me in the right direction of how to achieve the following:

Acrobat DC

I have a drop-down box which contains five items.  I have managed to change the colour of the text for each item when selected within the drop-down box.  What I would really like to do is to change the background colour of 5 different text fields as well.  So selecting item 1 turns the text red in the drop-down and also makes the background colour of a text field red.  selecting item 2 makes the text turn blue in the drop-down and also makes the background of an additional text field blue.  And so forth...  No selection means the colour is transparent.

I cannot get my head around this.  If anyone could please point me in the right direction I would be eternally grateful.

This topic has been closed for replies.
Correct answer postseventeen

Ok, I worked it out!

I have the code in the 'Custom calculation script' for the text box.  "input_project_1_early" is the drop down.

event.value=this.getField("input_project_1_early").value;

if (event.value=="MS")

    event.target.fillColor = color.blue;

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

    event.target.fillColor = color.magenta;

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

    event.target.fillColor = color.green;

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

    event.target.fillColor = color.red;

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

    event.target.fillColor = color.black;

else

    event.target.fillColor = color.transparent;

    event.target.fontColor = color.transparent;

works like a charm.

Thanks everyone for their replies.

3 replies

postseventeen
postseventeenAuthorCorrect answer
Participant
July 16, 2019

Ok, I worked it out!

I have the code in the 'Custom calculation script' for the text box.  "input_project_1_early" is the drop down.

event.value=this.getField("input_project_1_early").value;

if (event.value=="MS")

    event.target.fillColor = color.blue;

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

    event.target.fillColor = color.magenta;

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

    event.target.fillColor = color.green;

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

    event.target.fillColor = color.red;

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

    event.target.fillColor = color.black;

else

    event.target.fillColor = color.transparent;

    event.target.fontColor = color.transparent;

works like a charm.

Thanks everyone for their replies.

try67
Community Expert
Community Expert
July 16, 2019

Be aware that this line will be executed no matter what value is selected:

event.target.fontColor = color.transparent;

Luckily, there's no such property as "fontColor", so it doesn't really do anything... You should actually remove it.

postseventeen
Participant
July 16, 2019

Ah yes!  I have removed it - thanks

postseventeen
Participant
July 16, 2019

Is anyone able to tell me why this doesn't work?  From all the searching I have done I cannot see how else this is possible?

var newFillColor;

var value; 

if (value=="1") newFillColor = color.blue;

else if (value=="2") newFillColor = color.magenta;

else if (value=="3") newFillColor = color.green;

else if (value=="4") newFillColor = color.red;

else if (value=="5") newFillColor = color.yellow;

else newFillColor = color.transparent;  

event.target.fillColor = newFillColor;

this.getfield("input_project_1_early").value=value

Bernd Alheit
Community Expert
Community Expert
July 16, 2019

Where does you set the variable value?

try67
Community Expert
Community Expert
July 14, 2019

Simply set the field's fillColor property, just like you did with the textColor property to change the color of the text.

postseventeen
Participant
July 15, 2019

Hmmmm.  I still cannot get this to work.  I have entered the following into the custom validation script for the text box:

var indicator = this.getField("input_project_1_earlies").value; {

if (indicator=="1")

    event.target.fillColor = color.blue;

else if (indicator=="2")

    event.target.fillColor = color.magenta;

else if (indicator=="3")

    event.target.fillColor = color.green;

else if (indicator=="4")

   event.target.fillColor = color.red;

else "indicator_1_earlies".fillColor = color.transparent;

The dropdown box has the export value of each of the entries set as 1 to 5 and commit value immediately.

I feel like i'm going about this in completely the wrong way?

Bernd Alheit
Community Expert
Community Expert
July 16, 2019

Following line is incorrect:

else "indicator_1_earlies".fillColor = color.transparent;