Skip to main content
Luxurynightmares
Known Participant
September 15, 2022
Answered

A better script to hide and display a large amound of feilds

  • September 15, 2022
  • 2 replies
  • 797 views

Hello,

 

I have 20 fields (check boxes) that can be hidden/visible based upon the value of a textfield (Counter).  My script works fine, but, it's really long (161 lines) and there is a slight lag when I press a button that changes the value of the counter.  My script is a caculation script attached to a hidden textfield.   Is there a better way to write this script?  Here is what I have:

var c = this.getField("Counter");
var b1 = this.getField("Blah 1");
var b2 = this.getField("Blah 2");
var b3 = this.getField("Blah 3");

// etc etc. down to 20

if(c.value == "1"){
b1.display = display.visible;
}
else {
b1.display = display.hidden;
}

if(c.value == "2"){
b2.display = display.visible;
}
else {
b2.display = display.hidden;


// etc etc. down to 20

This topic has been closed for replies.
Correct answer Nesa Nurani

You can use this as custom calculation script of "Counter" field:

for(var i=1; i<=20; i++){
if(event.value == i)
this.getField("Blah "+i).display = display.visible;
else
this.getField("Blah "+i).display = display.hidden;}

2 replies

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
September 15, 2022

You can use this as custom calculation script of "Counter" field:

for(var i=1; i<=20; i++){
if(event.value == i)
this.getField("Blah "+i).display = display.visible;
else
this.getField("Blah "+i).display = display.hidden;}

Luxurynightmares
Known Participant
September 15, 2022

Thank you so much, this is perfect.

Bernd Alheit
Community Expert
Community Expert
September 15, 2022

You can build the field name. E.g.:

this.getField("Blah " + c.value)

Luxurynightmares
Known Participant
September 15, 2022

So basically delete variables b1-20? 
And change all  b1.display = display.visible; 
to this?   this.getField("Blah " + c.value).display = display.visible;