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

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

Community Beginner ,
Sep 15, 2022 Sep 15, 2022

Copy link to clipboard

Copied

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

TOPICS
How to , JavaScript , PDF forms

Views

315

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
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 15, 2022 Sep 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;}

Votes

Translate

Translate
Community Expert ,
Sep 15, 2022 Sep 15, 2022

Copy link to clipboard

Copied

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

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

Votes

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
community guidelines
Community Beginner ,
Sep 15, 2022 Sep 15, 2022

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Sep 15, 2022 Sep 15, 2022

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Sep 15, 2022 Sep 15, 2022

Copy link to clipboard

Copied

LATEST

Thank you so much, this is perfect.

Votes

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
community guidelines