Skip to main content
postseventeen
Participant
Jul 14, 2019 4:40 PM UTC
Answered

Dropdown box selection to change background color of text field

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 is closed to new replies. Start a new post to keep the conversation going.
postseventeen
postseventeenCorrect answer
Participant
Jul 16, 2019 10:50 PM UTC

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.

9 replies

postseventeen
postseventeenAuthorCorrect answer
Participant
Jul 16, 2019 10:50 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Jul 16, 2019 10:56 PM UTC

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.

Translate
postseventeen
Participant
Jul 16, 2019 11:15 PM UTC

Ah yes!  I have removed it - thanks

Translate
postseventeen
Participant
Jul 16, 2019 3:45 AM UTC

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

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 16, 2019 5:26 AM UTC

Where does you set the variable value?

Translate
try67
Community Expert
Community Expert
Jul 14, 2019 4:54 PM UTC

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

Translate
postseventeen
Participant
Jul 15, 2019 11:50 PM UTC

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?

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 16, 2019 9:01 AM UTC

Following line is incorrect:

else "indicator_1_earlies".fillColor = color.transparent;

Translate