Highlighted

Need help with javascript

Explorer ,
Nov 15, 2020

Copy link to clipboard

Copied

Can someone help me make these codes into one code?
I have ten fields named t1-t10,
in each field as custom calculation script I have these codes
in field1:
event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
in field2:
event.target.fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
in field3:
event.target.fillColor = this.getField("Text1").value >= 3 ? color.green : color.white;
...etc up to field 10:
event.target.fillColor = this.getField("Text1").value == 10 ? color.green : color.white;
I want to put them in one field and have as one code, I know how to make them like this:

event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
this.getField("t2").fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
...etc but I was wondering can it be done as one code something like:
var color = "";
for (var i=1; i<=10; i++) {
if (this.getField("t1"+i).value=="1") color++;
}
event.target.fillColor ? color.green : color.white;

Of course that doesn't work but you get idea what I'm looking for.

If you want to use it as validation script add this to try67 code:


if (event.value < 0 || event.value > 10) {
app.alert("Your text goes here.");
event.rc = false;
}

TOPICS
Acrobat SDK and JavaScript

Views

36

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

Need help with javascript

Explorer ,
Nov 15, 2020

Copy link to clipboard

Copied

Can someone help me make these codes into one code?
I have ten fields named t1-t10,
in each field as custom calculation script I have these codes
in field1:
event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
in field2:
event.target.fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
in field3:
event.target.fillColor = this.getField("Text1").value >= 3 ? color.green : color.white;
...etc up to field 10:
event.target.fillColor = this.getField("Text1").value == 10 ? color.green : color.white;
I want to put them in one field and have as one code, I know how to make them like this:

event.target.fillColor = this.getField("Text1").value >= 1 ? color.green : color.white;
this.getField("t2").fillColor = this.getField("Text1").value >= 2 ? color.green : color.white;
...etc but I was wondering can it be done as one code something like:
var color = "";
for (var i=1; i<=10; i++) {
if (this.getField("t1"+i).value=="1") color++;
}
event.target.fillColor ? color.green : color.white;

Of course that doesn't work but you get idea what I'm looking for.

If you want to use it as validation script add this to try67 code:


if (event.value < 0 || event.value > 10) {
app.alert("Your text goes here.");
event.rc = false;
}

TOPICS
Acrobat SDK and JavaScript

Views

37

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

Copy link to clipboard

Copied

Use this code as the custom Validation script of "Text1":

 

for (var i=1; i<=10; i++) {
	var f = this.getField("t"+i);
	f.fillColor = (Number(event.value)>=i) ? color.green : 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 15, 2020 0
Explorer ,
Nov 15, 2020

Copy link to clipboard

Copied

Thank you, you'r always so helpful, and code works nice but I have new problem now, in that field I was using field value is in range 0-10 but if I use code I can't use that option anymore. Can I add something to the code to make it range 0-10 again?

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

Copy link to clipboard

Copied

You can move that code to be a Calculation script, and then you'll still be able to use the built-in range Validation option.

 

And you're welcome!

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 15, 2020 0
Engaged ,
Nov 15, 2020

Copy link to clipboard

Copied

If you want to use it as validation script add this to try67 code:


if (event.value < 0 || event.value > 10) {
app.alert("Your text goes here.");
event.rc = false;
}

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