Skip to main content
June 23, 2020
Answered

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

  • June 23, 2020
  • 1 reply
  • 854 views

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.

This topic has been closed for replies.
Correct answer try67

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;

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 23, 2020

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;

June 23, 2020

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?

try67
Community Expert
Community Expert
June 23, 2020

Same concept:

 

var total = 0;

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