4 Replies Latest reply on Mar 17, 2015 1:43 PM by gkaiseril

    For loop not working

    Swatty43

      Hi Everyone,

       

      I am trying to use a simple for loop and it is not working. I have a group of drop down boxes that I have named Dropdown.A - Dropdown.F

       

      I am able to make it into an array using:

       

      var Dropdown = this.getField("Dropdown").getArray();

       

      however when I try and loop over the array using:

       

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

         console.println(Dropdown[i].name);

      }

       

      I get "Dropdown[i] is undefined" so it seems like the i is never being replaced by the correct number when looping.

       

      Has anyone ever seen this or know why this is happening?

       

      Thanks,

       

      Mike

        • 1. Re: For loop not working
          George_Johnson MVP & Adobe Community Professional

          That code works for me in a button's Mouse Up event, so either there's more to it or I don't understand how you're using it.

          • 2. Re: For loop not working
            gkaiseril MVP & Adobe Community Professional

            The getArray() method of the field object returns an array of the field objects for any child under the parent field. You children fields are named "Dropdown.A", "Dropdown.B", "Dropdown.C", "Dropdown.D", "Dropdown.e", and "Dropdown.F". Not "Dropdown.#" where # is a number from 0 - 4.

             

            The return value of "undefined" is because the field object you are trying to access, "Dropdown.0' does not exist.

             

            With your naming scheme because it is using alphabetical characters and not numbers.

             

             


            • 3. Re: For loop not working
              George_Johnson MVP & Adobe Community Professional

              GKaiseril wrote:

               

              You children fields are named "Dropdown.A", "Dropdown.B", "Dropdown.C", "Dropdown.D", "Dropdown.e", and "Dropdown.F". Not "Dropdown.#" where # is a number from 0 - 4.

               

              The return value of "undefined" is because the field object you are trying to access, "Dropdown.0' does not exist.

               

              With your naming scheme because it is using alphabetical characters and not numbers.

               

               

               

               

              That's not what the code does. As I mentioned earlier, it works fine, so there's something missing.

              • 4. Re: For loop not working
                gkaiseril MVP & Adobe Community Professional

                You might try:

                 

                function GetField(cName) {
                var oField = this.getField(cName);
                if(oField ==  null) {
                  var cMsg = "Error accessing field " + cName + "\n\n";
                  app.alert(cMsg, 1, 0);
                }
                return oField;
                }

                var Dropdown = GetField("Dropdown").getArray();
                for (var i = 0; i < Dropdown.length; i++){
                console.println(i + ": " + Dropdown[i].name);
                }

                 

                My guess there is a misspelling or capitalization error in your field name.