22 Replies Latest reply on Apr 2, 2013 12:36 PM by Anonymous.123

    Simple array comparison not working. Please HELP!

    Anonymous.123

      I have been struggling to solve this problem for a few days now, and I'm slowly losing my mind.

       

      • I am in Adobe Acrobat 10.
      • I have a group of button fields called: btn0, btn1, btn2, etc.
      • I have a group of text fields called: txt0, txt1, txt2, etc.
      • I have a script that takes the value of txt0 and makes it the caption for btn0, and so on.
      • I have a script that sets the MouseUp action of btn0 to setFocus to txt0, and so on.
      • These scripts work fine.
      • I have a script that takes the values of all the txt fields and puts them in an array, and sorts it.
      • I have a script that takes the array[0] item and makes it the caption for btn0, and so on (alphabetizing my list of buttons).
      • Those scripts work fine.
      • I am trying to compare the value of the array[0] item to each of the txt fields to find the match, and then set the MouseUp action of btn0 to setFocus to the matching txt field, and so on (so my alphabetized list points to the correct locations).
      • This is where I'm at a loss.

       

      Here is what I have:

       

      //specified the txt fields

      var txt0 = this.getField("Z name");

      var txt1 = this.getField("A name");

      //etc.

       

      //put their values into an array called rxArray

      var rxArray = [txt0.value, txt1.value]; //etc.

       

      //sorted the array

      rxArray.sort();

       

      //set the captions equal to the sorted array items

      for (var i = 0; i < 5; i++) {

          var b = ("btn" + i);

          this.getField(b).buttonSetCaption(rxArray[i]); //works fine; alphabetizes the list of buttons

       

          //below is what goes wrong

          for(var x = 0; x < 5; x++) {

              var r = ("txt" + x);

              if(rxArray[i] == r.value){

                  var s = (r + ".setFocus();");

                  this.getField(b).setAction("MouseUp", s);

                  }

              }

          }

       

      //end

       

      Here is what I know:

       

      The alphabetizing and labeling works fine, but the buttons' MouseUp scripts don't work at all. Nothing happens.

       

      If I change the following piece of the above script:

              if(rxArray[i] == r.value){

                  var s = (r + ".setFocus();");

                  this.getField(b).setAction("MouseUp", s);

       

      To this:

              if(rxArray[i] == txt1.value){

                  var s = (r + ".setFocus();");

                  this.getField(b).setAction("MouseUp", s);

       

      Because rxArray[0] does equal the value of txt1 ("A name"), then the MouseUp script for btn0 gets set to:

      txt4.setFocus();

       

      So I know that, each time the nested loop runs, the if statement is true, and the setFocus script updates. Until the end of the loop, leaving the setFocus as the last item run. So why doesn't my script work? It should only update the setFocus script IF the array item matches the txt field, and should set it to THAT txt field.

       

      Please please help. I know I'm missing something simple in there somewhere.

        • 1. Re: Simple array comparison not working. Please HELP!
          Test Screen Name Most Valuable Participant

          What does the JavaScript debugger say?  Looks like you are trying to use a string with the name of an element "txt1" just as if it were the element itself...

          • 2. Re: Simple array comparison not working. Please HELP!
            Anonymous.123 Level 1

            Debugger says:

             

            TypeError: r.setFocus is not a function

            1:Field:Mouse Up

            TypeError: r.setFocus is not a function

            1:Field:Mouse Up

             

            I agree that I might be trying to do that. Is there a better way to specific the element itself?

             

            Thank you.

            • 3. Re: Simple array comparison not working. Please HELP!
              Anonymous.123 Level 1

              So what you're suggesting is that rather than:

               

              r.value being equal to txt1.value, it is equal to "txt1".value

               

              But, here's the thing: in my other script, that works fine, I use:

               

              for (var i = 0; i < 5; i++) {

                  var b = ("btn" + i);

                  this.getField(b).buttonSetCaption(rxArray[i].value);

                  var r = ("txt" + x);

                  var s = (r + ".setFocus();");

                  this.getField(b).setAction("MouseUp", s);

               

              So here, the variable r is working as intended. It is referencing the element itself. But not when using r.value...

              • 4. Re: Simple array comparison not working. Please HELP!
                Anonymous.123 Level 1

                Solved:

                 

                I realized that I was generating script for each of the buttons without the variables they reference. So the script for btn0 became:

                 

                txt1.setFocus();

                 

                But that script by itself doesn't know what field txt1 is. I resolved the problem by writing out the script's basic functioning and passing in the field name. So the nested loop becomes:

                 

                for(var x = 0; x < 5; x++) {

                        var r = ("txt" + x);

                        if(rxArray[i] == r.value){

                            var s = ("this.getField('" + rxArray[i].name + "').setFocus();");

                            this.getField(b).setAction("MouseUp", s);

                            }

                        }

                 

                Which generates the following script for the button itself:

                 

                this.getField('A name').setFocus();

                 

                NB: This problem probably could have also been solved by using document-level variables, but this worked for me and I didn't try other solutions.

                • 5. Re: Simple array comparison not working. Please HELP!
                  try67 MVP & Adobe Community Professional

                  Your new script doesn't make much sense, either.

                  The variable "r" is just a string, and therefore has no "value" property.

                  • 6. Re: Simple array comparison not working. Please HELP!
                    Anonymous.123 Level 1

                    @Try69:

                     

                    You're correct. Sorry, I forgot to include the other change I made, which was to create a second array for just the fields (not their values):

                     

                    var newArray = [txt0, txt1];

                     

                    And then make the comparison to the value of those array items:

                     

                    for (var i = 0; i < 5; i++) {

                        var b = ("btn" + i);

                        this.getField(b).buttonSetCaption(rxArray[i]);

                     

                        for(var x = 0; x < 5; x++) {

                           if(rxArray[i] == newArray[x].value){

                                var s = ("this.getField('" + newArray[x].name + "').setFocus();");

                                this.getField(b).setAction("MouseUp", s);

                                }

                            }

                        }

                    • 7. Re: Simple array comparison not working. Please HELP!
                      try67 MVP & Adobe Community Professional

                      Why are you iterating to a fixed number, instead of to the length of your array?

                      • 8. Re: Simple array comparison not working. Please HELP!
                        Anonymous.123 Level 1

                        @Try67:

                         

                        That's a good question. I was running into some other issues and have revamped my code. Here is what I have in my test file:

                         

                        A list of five buttons and a list of five text fields. One additional button that sets the focus to the next empty text field to add a new item, and two additional buttons, one that sorts my list alphabetically, and one that unsorts it.

                         

                        2.PNG

                        with the following field names

                        3.PNG

                         

                        The sort button calls function sortName and the unsort calls function sortNumber (the order of entry).

                         

                        Here are those scripts in final form:

                         

                        function sortName() {

                         

                        //first reset the captions for the buttons to blank

                        for (var a = 0; a < 5; a++) {

                            var b = ("btn" + a);

                            this.getField(b).buttonSetCaption("");

                        }

                         

                        var txt0 = this.getField("t0");

                        var txt1 = this.getField("t1");

                        var txt2 = this.getField("t2");

                        var txt3 = this.getField("t3");

                        var txt4 = this.getField("t4");

                         

                        var rxArray = [txt0.value, txt1.value, txt2.value, txt3.value, txt4.value];

                        for(var m = rxArray.length - 1; m > -1; m--){

                            if(rxArray[m] == ""){

                                rxArray.splice(m, 1);

                            }

                        }

                        rxArray.sort();

                         

                        var newArray = [txt0, txt1, txt2, txt3, txt4];

                        for(var n = newArray.length - 1; n > -1; n--){

                            if(newArray[n].value == ""){

                                newArray.splice(n, 1);

                            }

                        }

                         

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

                            var b = ("btn" + i);

                            this.getField(b).buttonSetCaption(rxArray[i]);

                            for (var x = 0; x < newArray.length; x++) {

                                if(rxArray[i] == newArray[x].value){

                                    var s = ("this.getField('" + newArray[x].name + "').setFocus();");

                                    this.getField(b).setAction("MouseUp", s);

                                    }

                                }

                            }

                         

                        }

                        //end

                         

                        function sortNumber() {

                         

                        var txt0 = this.getField("t0");

                        var txt1 = this.getField("t1");

                        var txt2 = this.getField("t2");

                        var txt3 = this.getField("t3");

                        var txt4 = this.getField("t4");

                         

                        var newArray = [txt0, txt1, txt2, txt3, txt4];

                         

                        for (var x = 0; x < newArray.length; x++) {

                            var b = ("btn" + x);

                            this.getField(b).buttonSetCaption(newArray[x].value);

                            var s = ("this.getField('" + newArray[x].name + "').setFocus();");

                            this.getField(b).setAction("MouseUp", s);

                            }

                         

                        }

                        //end

                         

                        As you can see, I've used the array lengths rather than fixed numbers, except when clearing the button values. I use a number there because there is no array to reference and didn't feel like making an array just for that. The number of buttons won't change.

                         

                        I've also added in a splice() method to remove the blank entries from my arrays when appropriate (making using the array length even more important).

                         

                        The result of the sort is:

                        1.PNG

                         

                        The only quirk I've found in all this is with the Add New button, which calls function addNew, which is:

                         

                        function addNew() {

                         

                        var txt0 = this.getField("t0");

                        var txt1 = this.getField("t1");

                        var txt2 = this.getField("t2");

                        var txt3 = this.getField("t3");

                        var txt4 = this.getField("t4");

                         

                        var newArray = [txt0, txt1, txt2, txt3, txt4];

                         

                        for (var i =  newArray.length - 1; i > -1 ; i--) {

                            if (newArray[i].value == "") {

                                newArray[i].setFocus();

                                }

                            }

                         

                        }

                        //end

                         

                        For this, I would have though that running through the array from start to finish looking for the first empty text field and setting the focus to it would have been correct. But that resulted in the last empty text field being focused. So I reversed the for loop to run finish to start, and the result was that the first empty field was focused. Not sure why that is...

                        • 9. Re: Simple array comparison not working. Please HELP!
                          try67 MVP & Adobe Community Professional

                          I don't see why you would need arrays at all... Let's take sortNumber, for example.

                          You can basically replace all of that code with just this:

                           

                          for (var x = 0; x < 5; x++) {

                              var t = this.getField("t"+x); // the text field

                              var b = this.getField("btn" + x); // the button

                              b.buttonSetCaption(t.value);

                              var s = "this.getField('" + t.name + "').setFocus();";

                              b.setAction("MouseUp", s);

                          }

                           

                          The same applies for the rest of your code as well.

                           

                          As for the addNew function, you need to add a break command inside the if-statement to prevent it from continuing further after the first blank field was found.

                          • 10. Re: Simple array comparison not working. Please HELP!
                            Anonymous.123 Level 1

                            That's very simple and clear. Thank you! That will help me a lot. However, one catch is that this is my test file. My real file has text field names that aren't numbers, they are unique identifiers. So the only way to cycle through them would be with an array, right?

                             

                            I just looked up break/continue commands, and that will help me considerably as well.

                             

                            I'm self taught at javascripting, and its because of forums like this (this one in particular) and posters like you that my education has been possible. Thank you!

                            • 11. Re: Simple array comparison not working. Please HELP!
                              try67 MVP & Adobe Community Professional

                              Yes, if the form fields are not named in consistent manner then you would need to use an array.

                               

                              I'm also a self-taught JS programmer,  and happy to pass some knowledge along! Keep it up...

                              1 person found this helpful
                              • 12. Re: Simple array comparison not working. Please HELP!
                                Anonymous.123 Level 1

                                For sortName, don't I need to use an array anyway in order to sort the t.name alphabetically before assigning them to the buttons?

                                • 13. Re: Simple array comparison not working. Please HELP!
                                  try67 MVP & Adobe Community Professional

                                  If the names are "t0", "t1", "t2", "t3", etc., what's to sort? They're already sorted.

                                  • 14. Re: Simple array comparison not working. Please HELP!
                                    Anonymous.123 Level 1

                                    Sorry, I meant the t.value. The caption for btn0 could be t0.value or t4.value depending on the alphabetical ordering of those values.

                                    • 15. Re: Simple array comparison not working. Please HELP!
                                      try67 MVP & Adobe Community Professional

                                      Sorry, I guess I didn't understand what you're trying to achieve, then.

                                      • 16. Re: Simple array comparison not working. Please HELP!
                                        Anonymous.123 Level 1

                                        I tried to use this for sortName:

                                         

                                        for (var a = 0; a < 5; a++) {

                                            var b = this.getField("btn" + a);

                                            b.buttonSetCaption(""); // reset the button captions

                                            }

                                         

                                        var arr = [] // new empty array

                                        for (var x = 0; x < 5; x++) {

                                            var t = this.getField("t" + x);

                                            if (t == null) continue;

                                            arr.push(t); // build the array with the text fields if they aren't empty

                                            }

                                        arr.sort();

                                         

                                        for (var x = 0; x < 5; x++) {

                                            var b = this.getField("btn" + x);

                                            b.buttonSetCaption(arr[x].value);

                                            var s = "this.getField('" + arr[x].name + "').setFocus();";

                                            b.setAction("MouseUp", s);

                                            }

                                        • 17. Re: Simple array comparison not working. Please HELP!
                                          Anonymous.123 Level 1

                                          Cleaned this up a bit:

                                           

                                          for (var a = 0; a < 5; a++) {

                                              var b = this.getField("btn" + a);

                                              b.buttonSetCaption(""); // reset the button captions

                                              }

                                           

                                          var arr = []; // new empty array

                                          for (var x = 0; x < 5; x++) {

                                              var t = this.getField("t" + x);

                                              if (t.value == null) continue;

                                              arr.push(t); // build the array with the text fields if they aren't empty

                                              }

                                          arr.sort();

                                           

                                          for (var x = 0; x < arr.length; x++) {

                                              var b = this.getField("btn" + x);

                                              b.buttonSetCaption(arr[x].value);

                                              var s = "this.getField('" + arr[x].name + "').setFocus();";

                                              b.setAction("MouseUp", s);

                                              }

                                           

                                          Still not working.

                                          • 18. Re: Simple array comparison not working. Please HELP!
                                            Anonymous.123 Level 1

                                            Okay. I looked at some of your other examples to other people's questions, and I put this together:

                                             

                                            function sortName() {

                                             

                                            var arr = [];

                                            for (var x = 0; x < 5; x++) {

                                                var t = this.getField("t" + x);

                                                if (t.value == "") continue;

                                                arr.push(t.value);

                                                }

                                            arr.sort();

                                             

                                            for (var x = 0; x < 5; x++) {

                                                var b = this.getField("btn" + x);   

                                                if(x < arr.length) {

                                                    b.buttonSetCaption(arr[x]);

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

                                                        var t = this.getField("t" + i);

                                                        if(arr[x] == t.value) {

                                                            var s = ("this.getField('" + t.name + "').setFocus();");

                                                            b.setAction("MouseUp", s);

                                                            }

                                                        } 

                                                    } else {

                                                    b.buttonSetCaption("");

                                                    b.setAction("MouseUp", " ");

                                                    } 

                                                }

                                            }

                                            //end

                                             

                                            This works, but only sometimes. If I enter 3 names (Ben, John, Mike) into three of the text fields and run the sort, it correctly sorts the names as the captions for the buttons, starting with btn0, and it makes all empty buttons have no script. If I click the "Ben" button, it sets the focus to the "Ben" text field, correctly. It works correctly for each name. But, if I change the names in the text fields and try a new sort, depending on the names, the set focus doesn't work right. Sometimes the first button sets focus incorrectly to the second text field, sometimes the fourth button incorrectly sets focus to the third button. Etc. There doesn't seem to be rhyme or reason to it, it changes each time I try new names.

                                             

                                            What am I missing here?

                                            • 19. Re: Simple array comparison not working. Please HELP!
                                              try67 MVP & Adobe Community Professional

                                              Sorry, but I'm having a hard time understanding what you're trying to

                                              achieve here...

                                              One possible problem I'm seeing is that you're accessing arr[x] with x

                                              going from 0 to 4, but there's no guarantee that arr has 5 items in it.

                                               

                                              Do you have the option to show the JS console on warnings and errors

                                              selected? This is a vital setting when developing code as it helps identify

                                              and locate errors.

                                              • 20. Re: Simple array comparison not working. Please HELP!
                                                Anonymous.123 Level 1

                                                Two updates:

                                                 

                                                The irregular behavior only occurs if there is an empty text field between two or more full fields. So if the list is:

                                                 

                                                Roger

                                                Henry

                                                Dave

                                                Larry

                                                [blank]

                                                 

                                                It will sort correctly and link correctly. But if the list is:

                                                 

                                                Roger

                                                [blank]

                                                Henry

                                                Dave

                                                Larry

                                                 

                                                The sorting works fine but the linking doesn't. The linking is only ever messed up for one item, but its not consistently the last item in the order or the last item alphabetically or any other consistent occurence. What is happening?

                                                 

                                                Additionally, the sort is sorting by capitalization. So a, B, b, A becomes A, B, a, b. Can I change that?

                                                • 21. Re: Simple array comparison not working. Please HELP!
                                                  Anonymous.123 Level 1

                                                  My debugger just says:

                                                   

                                                  Acrobat JavaScript Debugger Functions Version 10.0

                                                  Acrobat EScript Built-in Functions Version 10.0

                                                  Acrobat SOAP 10.0

                                                  • 22. Re: Simple array comparison not working. Please HELP!
                                                    Anonymous.123 Level 1

                                                    I'm trying to create a directory. I have file with several pages. Each page contains contact information for a person, along with other information. They are entered in the ordered they are interviewed. I'd like to create a directory of buttons at the start of the file to jump to a person's page. I want the list to sort alphabetically, and have the alphabetized list link correct to the listed person's page.

                                                     

                                                    I also want a button on that directly page to jump to the next empty page to add a new person, but I've figured out the script for that.

                                                     

                                                    Thanks again for your continued help on this.