Highlighted

Trying to change text color and fill color simutaneously - Help Please!

New Here ,
Nov 18, 2020

Copy link to clipboard

Copied

Hello Friends,

 

I am trying to write a script that will change a fill color based on a calculated field's event value, but I am having trouble getting the effected field to change both colors. This is the script I currently have that will not work;

 

if (Number(event.value)>1)
app.alert("Total Beneficiary Allocation exceeds 100%");
if(event.value == 0)
event.target.fillColor = color.white;
else if(event.value < 1)
event.target.fillColor = color.yellow;
else if(event.value > 1)
event.target.fillColor = color.red && event.target.textColor = color.white;
else
event.target.fillColor = color.green;

 

The script works perfectly fine when I remove "&& event.target.textColor = color.white", but gives me "invalid assignment" when entered. All help would be greatly appreciated!

 

Thank you,

Eric

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Its a matter of using the correct "if" block syntax.

 

if(event.value == 0)
{
    event.target.fillColor = color.white;
    event.target.textColor = color.black;
}
else if(event.value < 1)
{
    event.target.fillColor = color.yellow;
    event.target.textColor = color.black;
}
else if(event.value > 1)
{
    app.alert("Total Beneficiary Allocation exceeds 100%");
    event.target.fillColor = color.red;
    event.target.textColor = color.white;
}
else
{
    event.target.fillColor = color.green;
    event.target.textColor = color.black;
}

 

 

Notice how the code for each if statement is enclosed in curly brackets. And both colors are set in every block. 

You can read more about using "if" statements here:

https://acrobatusers.com/tutorials/conditional-execution/

 

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

32

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Trying to change text color and fill color simutaneously - Help Please!

New Here ,
Nov 18, 2020

Copy link to clipboard

Copied

Hello Friends,

 

I am trying to write a script that will change a fill color based on a calculated field's event value, but I am having trouble getting the effected field to change both colors. This is the script I currently have that will not work;

 

if (Number(event.value)>1)
app.alert("Total Beneficiary Allocation exceeds 100%");
if(event.value == 0)
event.target.fillColor = color.white;
else if(event.value < 1)
event.target.fillColor = color.yellow;
else if(event.value > 1)
event.target.fillColor = color.red && event.target.textColor = color.white;
else
event.target.fillColor = color.green;

 

The script works perfectly fine when I remove "&& event.target.textColor = color.white", but gives me "invalid assignment" when entered. All help would be greatly appreciated!

 

Thank you,

Eric

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Its a matter of using the correct "if" block syntax.

 

if(event.value == 0)
{
    event.target.fillColor = color.white;
    event.target.textColor = color.black;
}
else if(event.value < 1)
{
    event.target.fillColor = color.yellow;
    event.target.textColor = color.black;
}
else if(event.value > 1)
{
    app.alert("Total Beneficiary Allocation exceeds 100%");
    event.target.fillColor = color.red;
    event.target.textColor = color.white;
}
else
{
    event.target.fillColor = color.green;
    event.target.textColor = color.black;
}

 

 

Notice how the code for each if statement is enclosed in curly brackets. And both colors are set in every block. 

You can read more about using "if" statements here:

https://acrobatusers.com/tutorials/conditional-execution/

 

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

33

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 18, 2020 0
Engaged ,
Nov 18, 2020

Copy link to clipboard

Copied

Try like this:

if (Number(event.value)>1)
app.alert("Total Beneficiary Allocation exceeds 100%");
if(event.value == 0){
event.target.fillColor = color.white;}
else if(event.value < 1){
event.target.fillColor = color.yellow;}
else if(event.value > 1){
event.target.fillColor = color.red;
event.target.textColor = color.white;}
else
event.target.fillColor = color.green;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2020 0
Adobe Community Professional ,
Nov 18, 2020

Copy link to clipboard

Copied

Its a matter of using the correct "if" block syntax.

 

if(event.value == 0)
{
    event.target.fillColor = color.white;
    event.target.textColor = color.black;
}
else if(event.value < 1)
{
    event.target.fillColor = color.yellow;
    event.target.textColor = color.black;
}
else if(event.value > 1)
{
    app.alert("Total Beneficiary Allocation exceeds 100%");
    event.target.fillColor = color.red;
    event.target.textColor = color.white;
}
else
{
    event.target.fillColor = color.green;
    event.target.textColor = color.black;
}

 

 

Notice how the code for each if statement is enclosed in curly brackets. And both colors are set in every block. 

You can read more about using "if" statements here:

https://acrobatusers.com/tutorials/conditional-execution/

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2020 0
New Here ,
Nov 18, 2020

Copy link to clipboard

Copied

Works perfectly! Thank you for your help and wisdom!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2020 1
Adobe Community Professional ,
Nov 18, 2020

Copy link to clipboard

Copied

BTW:

Your original code will work with a small adjustment. The reported error is caused by an order of execution issue, i.e. an operator precedence issue. 

The "&&" on the left side evaluates before the "=", so the assignment is to a static value, not the "textColor" property. 

 

But this code change will work because the parenthese force the order of execution.

(event.target.fillColor = color.red) && (event.target.textColor = color.white);

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 18, 2020 0