Change text color using javascript

Explorer ,
Aug 15, 2020

Copy link to clipboard

Copied

I tried everything I can think of and just can't get this to work.In my "field1" validate tab I use this custom code

if(event.value > 0){event.target.textColor = color.blue;
}else if(event.value < 0){event.target.textColor = color.red;
}else if(event.value == 0){event.target.textColor = color.white;
}

field1 get it's value from another field. I want it to show if 0= white, if bigger then 0=blue, if lower then 0=red but the colors are mixed up,for example if number is 5 it should be blue but is white, 0 is red and -2 is blue but in field properties it shows correct color ( example in the field1 value is 5 it show white color but in properties it's blue) .If I enter value manually it doesn't change colors at all.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Try this code:

 

var v = Number(this.getField("fieldname").valueAsString);
if (v > 0){
	event.target.textColor = color.blue;
} else if (v < 0){
	event.target.textColor = color.red;
} else if (v == 0){
	event.target.textColor = color.white;
}
event.value = v;

 

If it doesn't work check the JS Console for error messages.

TOPICS
Acrobat SDK and JavaScript, How to

Views

66

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

Change text color using javascript

Explorer ,
Aug 15, 2020

Copy link to clipboard

Copied

I tried everything I can think of and just can't get this to work.In my "field1" validate tab I use this custom code

if(event.value > 0){event.target.textColor = color.blue;
}else if(event.value < 0){event.target.textColor = color.red;
}else if(event.value == 0){event.target.textColor = color.white;
}

field1 get it's value from another field. I want it to show if 0= white, if bigger then 0=blue, if lower then 0=red but the colors are mixed up,for example if number is 5 it should be blue but is white, 0 is red and -2 is blue but in field properties it shows correct color ( example in the field1 value is 5 it show white color but in properties it's blue) .If I enter value manually it doesn't change colors at all.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Try this code:

 

var v = Number(this.getField("fieldname").valueAsString);
if (v > 0){
	event.target.textColor = color.blue;
} else if (v < 0){
	event.target.textColor = color.red;
} else if (v == 0){
	event.target.textColor = color.white;
}
event.value = v;

 

If it doesn't work check the JS Console for error messages.

TOPICS
Acrobat SDK and JavaScript, How to

Views

67

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
Aug 15, 2020 0
Adobe Community Professional ,
Aug 15, 2020

Copy link to clipboard

Copied

How does get field1 the value from the other field?

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...
Aug 15, 2020 0
Explorer ,
Aug 15, 2020

Copy link to clipboard

Copied

With custom calculation script 

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

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...
Aug 15, 2020 0
Adobe Community Professional ,
Aug 15, 2020

Copy link to clipboard

Copied

Use your other script also there.

 

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...
Aug 15, 2020 0
Explorer ,
Aug 15, 2020

Copy link to clipboard

Copied

I try and it didn't change anything.

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...
Aug 15, 2020 0
Most Valuable Participant ,
Aug 15, 2020

Copy link to clipboard

Copied

Try this code:

 

var v = Number(this.getField("fieldname").valueAsString);
if (v > 0){
	event.target.textColor = color.blue;
} else if (v < 0){
	event.target.textColor = color.red;
} else if (v == 0){
	event.target.textColor = color.white;
}
event.value = v;

 

If it doesn't work check the JS Console for error messages.

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...
Aug 15, 2020 0
Marietaa LATEST
Explorer ,
Aug 15, 2020

Copy link to clipboard

Copied

Thank you that worked.

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...
Aug 15, 2020 0