Skip to main content
Alliosaaa
Inspiring
August 12, 2019
Question

Help with "For" Loop... Not Checking All Boxes in Array

  • August 12, 2019
  • 1 reply
  • 1700 views

Hi there! I am having trouble with a script I'm working on and hoping I can get some advice.

I have a form with several fields that are either visible or hidden depending on the state chosen by the user. The user may select different state forms by checking check boxes, then hits a button to append those state forms to the document.

When a user selects their state, a function runs to set their location (this consists of making state specific fields visible, and checking all boxes for each state form by default). The checkbox portion is where I'm running into issues. My loop will only check or uncheck the first box, then stop. Here is the portion of code in question:

var Location = this.getField("LOCATION").value;

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var ME4 = this.getField("ME_4");

var ME5 = this.getField("ME_5");

var NH1 = this.getField("NH_1");

var VT1 = this.getField("VT_1");

var VT2 = this.getField("VT_2");

var allChecks = [ME1, ME2, ME3, ME4, ME5, NH1, VT1, VT2];

var StateChecks = [];

//Loop through fields and push to new array depending on state chosen

for (i = 0; i < allChecks.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allChecks.name.indexOf("ME") !== -1) {

            StateChecks.push(allChecks);

        }

    }

}

for (i=0; i < allChecks.length; i++) {

    allChecks.checkThisBox(0,false);

}

for (i=0; i < StateChecks.length; i++) {

    StateChecks.checkThisBox(0,true);

}

}

In testing snippets of this code in another document, I was able to get the following to work properly and check all three boxes.

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var allChecks = [ME1, ME2, ME3];

for (i=0; i < allChecks.length; i++) {

allChecks.checkThisBox(0,true);

}

However, even testing just that small snippet in the current form I'm working on, it still only checks one of the boxes.

Any advice would be greatly appreciated

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
August 12, 2019

I don't understand the logic behind your code. The fields that don't have "ME" will always be un-ticked, no matter the value of Location... What's the point of that? Could you maybe explain (in words) what you're trying to achieve, first?

Alliosaaa
AlliosaaaAuthor
Inspiring
August 12, 2019

Hi there - apologies for the confusion - I tried to only show the most relevant areas of the code and in so doing didn't fully explain that I already have "KPOR" chosen as a location in my document while I'm testing in the console. The results are the same no matter which location I choose - only one box gets checked (either ME_1, NH_1 or VT_1).

All state specific fields (which consist of both checkboxes and buttons) are hidden prior to choosing a location, and need to be toggled to "visible" / toggled to be checked if they are checkboxes. I start by hiding all the state fields and unchecking all boxes - just in case something has hiccuped up until this point - then displaying/checking only the fields that pertain to that state.

Here is the entire snippet...

var Location = this.getField("LOCATION").value;

var MEHead1 = this.getField("ME_HEADER1");

var MEHead2 = this.getField("ME_HEADER2");

var MEForms = this.getField("ME_FORMS");

var ME1 = this.getField("ME_1");

var ME2 = this.getField("ME_2");

var ME3 = this.getField("ME_3");

var ME4 = this.getField("ME_4");

var ME5 = this.getField("ME_5");

var NHHead1 = this.getField("NH_HEADER1");

var NHHead2 = this.getField("NH_HEADER2");

var NHForms = this.getField("NH_FORMS");

var NH1 = this.getField("NH_1");

var VTHead1 = this.getField("VT_HEADER1");

var VTHead2 = this.getField("VT_HEADER2");

var VTForms = this.getField("VT_FORMS");

var VT1 = this.getField("VT_1");

var VT2 = this.getField("VT_2");

var allFields = [MEHead1, MEHead2, MEForms, ME1, ME2, ME3, ME4, ME5, NHHead1, NHHead2, NHForms, NH1, VTHead1, VTHead2, VTForms, VT1, VT2];

var allChecks = [ME1, ME2, ME3, ME4, ME5, NH1, VT1, VT2];

var StateFields = [];

var StateChecks = [];

//Loop through fields and push to new array depending on state chosen

for (i = 0; i < allFields.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allFields.name.indexOf("ME") !== -1) {

            StateFields.push(allFields);

        }

    } else if (Location == "KCON") {

        if (allFields.name.indexOf("NH") !== -1) {

            StateFields.push(allFields);

        }

    } else if (Location == "KBUR" || Location == "KRUT") {

        if (allFields.name.indexOf("VT") !== -1) {

            StateFields.push(allFields);

        }

    }

}

for (i = 0; i < allChecks.length; i++) {

    if (Location == "KPOR" || Location == "KBAN") {

        if (allChecks.name.indexOf("ME") !== -1) {

            StateChecks.push(allChecks);

        }

    } else if (Location == "KCON") {

        if (allChecks.name.indexOf("NH") !== -1) {

            StateChecks.push(allChecks);

        }

    } else if (Location == "KBUR" || Location == "KRUT") {

        if (allChecks.name.indexOf("VT") !== -1) {

            StateChecks.push(allChecks);

        }

    }

}

for (i=0; i < allFields.length; i++) {

    allFields.display = display.hidden;

}

for (i=0; i < allChecks.length; i++) {

    allChecks.checkThisBox(0,false);

}

for (i=0; i < StateFields.length; i++) {

    StateFields.display = display.visible;

}

for (i=0; i < StateChecks.length; i++) {

    StateChecks.checkThisBox(0,true);

}

try67
Community Expert
Community Expert
August 12, 2019

You can improve the code, but it seems like it should work.

Are there any error messages in the JS Console when you run it?