Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need help with javascript

Explorer ,
Nov 15, 2020 Nov 15, 2020

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.

TOPICS
Acrobat SDK and JavaScript
588
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 15, 2020 Nov 15, 2020

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;
}
Translate
Community Expert , Nov 15, 2020 Nov 15, 2020

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;
}

Translate
Community Expert ,
Nov 15, 2020 Nov 15, 2020

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;
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 15, 2020 Nov 15, 2020

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2020 Nov 15, 2020

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2020 Nov 15, 2020
LATEST

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;
}

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