6 Replies Latest reply on May 11, 2014 12:18 PM by George_Johnson

    Script seems to pause each time a value is commited

    MatLac Level 1

      I created a simple script for copying the values of line "EVEN" to line "i" inside a button

      var i = event.target.name.charAt(event.target.name.length-1);

      this.getField("OPE003."+i+".NOCIVIQUE").value = this.getField("OPE003.EVEN.NOCIVIQUE").value;

      this.getField("OPE003."+i+".TYPE.RUE").value = this.getField("OPE003.EVEN.TYPE.RUE").value;

      this.getField("OPE003."+i+".RUE").value = this.getField("OPE003.EVEN.RUE").value;

      this.getField("OPE003."+i+".APP").value = this.getField("OPE003.EVEN.APP").value;

      this.getField("OPE003."+i+".ARRONDISSEMENT").value = this.getField("OPE003.EVEN.ARR").value;

      this.getField("OPE003."+i+".CP").value = this.getField("OPE003.EVEN.CP").value;

       

      the strange behavior is that instead of copying all the values at the same time, it copies only the first field.  if I press again, it copies the second one, on so on.

       

      I tryied my button in an earlier version of the same document and it works great.  Now I think the only difference between the earlier and new version is I introduced a bunch of keystroke and validation scripts testing regular expressions when entering data in some fields that are not even those listed above.  There is no error in the console window and every value is transfered one after the other if I press the button 6 times.

       

      What line of code have I possibly introduced that can stop such a simple button from working?

        • 1. Re: Script seems to pause each time a value is commited
          try67 MVP & Adobe Community Professional

          Check the JS Console (Ctrl+J) for errors. My guess is the name of one of

          your fields is wrong...

          • 2. Re: Script seems to pause each time a value is commited
            MatLac Level 1

            I found out that the problem seems to be on page 4 of my 6 pages document.  If I remove this page, the button works.  I'm investigating

            • 3. Re: Script seems to pause each time a value is commited
              MatLac Level 1

              I narrowed the error to 2 single fields holding similar calculating scripts on page 4.  they use the same variable which is "i".  it might be the problem.

              • 4. Re: Script seems to pause each time a value is commited
                Test Screen Name Most Valuable Participant

                Be very sure to use "var" on your first use of i in each calculation script. See Why You Should Use the Var Keyword in JavaScript - O'Reilly Answers

                • 5. Re: Script seems to pause each time a value is commited
                  MatLac Level 1

                  i changed "i" to "k" and the problem is solved.  I looked at the to calculation script interfering and as said, I did not use "var".  I added "var" but I might have made that mistake at other places as it did not solve the problem.

                  • 6. Re: Script seems to pause each time a value is commited
                    George_Johnson MVP & Adobe Community Professional

                    All variables declared outside of a function or used without declaring them first have document-wide scope. Since your code apparently triggers code in a different field that used the same variable name, there was a conflict. Changing the variable names is one approach, but it can be difficult to manage and it is much better to explicitly limit the scope of variables to only what is necessary. Normally you'd do this by placing as much code as you can into document-level JavaScripts as functions that get called in field events. But another way to do this is to place the code in field events inside anonymous functions that call themselves when the field script is triggered, something like:

                     

                    // Place the code in an anonymous function and call it when this script is triggered.

                    (function () {

                     

                        var i = event.target.name.charAt(event.target.name.length-1);

                        this.getField("OPE003."+i+".NOCIVIQUE").value = this.getField("OPE003.EVEN.NOCIVIQUE").value;

                        this.getField("OPE003."+i+".TYPE.RUE").value = this.getField("OPE003.EVEN.TYPE.RUE").value;

                        this.getField("OPE003."+i+".RUE").value = this.getField("OPE003.EVEN.RUE").value;

                        this.getField("OPE003."+i+".APP").value = this.getField("OPE003.EVEN.APP").value;

                        this.getField("OPE003."+i+".ARRONDISSEMENT").value = this.getField("OPE003.EVEN.ARR").value;

                        this.getField("OPE003."+i+".CP").value = this.getField("OPE003.EVEN.CP").value;

                     

                    })();

                     

                    If any of this code causes a script in another field that uses the same variable name to get triggered, then there won't be any conflict since the scope of the variables is local to the functions they are declared in.

                     

                    This is convenient if you don't want to bother creating a named function in a document JavaScript and is in a form that is easy to convert to a named function if you ever want to. The code inside the function will work unchanged, you just have to slightly modify the first and last lines.