Highlighted

How to change fields with similar names in an easier way?

Explorer ,
Jun 23, 2020

Copy link to clipboard

Copied

Hello.


I have 10 fields with very similar names, the beginning of the name is identical, what changes is only the end, which goes from number 1 to 10.

 

I want to create two buttons:
1) By clicking on this button, the display of fields 1 to 5 is visible. And the display of fields 6 to 10 is hidden.
2) The opposite happens. Display of fields 1 to 5 is hidden. And the display of fields 6 to 10 is visible.

 

Is there an easy way to do this?
I was doing it this way:
this.getField("Field1").display = display.visible;
this.getField("Field2").display = display.visible;
And etc., one line for each field.

 

But this seems to be a very inefficient way.

 

Thank you.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You can use a loop, like this:

for (var i=1; i<=5; i++) this.getField("Field"+i).display = display.visible;

for (var i=6; i<=10; i++) this.getField("Field"+i).display = display.hidden;

Topics

Acrobat SDK and JavaScript, PDF forms

Views

102

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

How to change fields with similar names in an easier way?

Explorer ,
Jun 23, 2020

Copy link to clipboard

Copied

Hello.


I have 10 fields with very similar names, the beginning of the name is identical, what changes is only the end, which goes from number 1 to 10.

 

I want to create two buttons:
1) By clicking on this button, the display of fields 1 to 5 is visible. And the display of fields 6 to 10 is hidden.
2) The opposite happens. Display of fields 1 to 5 is hidden. And the display of fields 6 to 10 is visible.

 

Is there an easy way to do this?
I was doing it this way:
this.getField("Field1").display = display.visible;
this.getField("Field2").display = display.visible;
And etc., one line for each field.

 

But this seems to be a very inefficient way.

 

Thank you.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You can use a loop, like this:

for (var i=1; i<=5; i++) this.getField("Field"+i).display = display.visible;

for (var i=6; i<=10; i++) this.getField("Field"+i).display = display.hidden;

Topics

Acrobat SDK and JavaScript, PDF forms

Views

103

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
Jun 23, 2020 2
Most Valuable Participant ,
Jun 23, 2020

Copy link to clipboard

Copied

You can use a loop, like this:

for (var i=1; i<=5; i++) this.getField("Field"+i).display = display.visible;

for (var i=6; i<=10; i++) this.getField("Field"+i).display = display.hidden;

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...
Jun 23, 2020 3
Explorer ,
Jun 23, 2020

Copy link to clipboard

Copied

Thank you! It is exactly what I needed.

Another thing ... and if I need to sum these same 10 fields and insert the value in a variable, how should I do it?

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...
Jun 23, 2020 2
try67 LATEST
Most Valuable Participant ,
Jun 23, 2020

Copy link to clipboard

Copied

Same concept:

 

var total = 0;

for (var i=1; i<=10; i++) total+=Number(this.getField("Field"+i).valueAsString);

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...
Jun 23, 2020 3