4 Replies Latest reply on Nov 3, 2016 2:13 PM by davidm76220796

    document javascripts

    davidm76220796 Level 1

      I am trying to create a Document Javascript which will define the fields so I don't have to define the fields each time, only call the function.

       

      It keeps throwing a syntax error.

       

      Help, Please.

       

      function AddPressure()

      {

      var Red10=this.getField("Red10")value;

      var Red20=this.getField("Red20")value;

      var Red30=this.getField("Red30")value;

      var Red40=this.getField("Red40")value;

      var Brown10=this.getField("Brown10")value;

      var Brown20=this.getField("Brown20")value;

      var Brown30=this.getField("Brown30")value;

      var Brown40=this.getField("Brown40")value;

      var Grey10=this.getField("Grey10")value;

      var Grey20=this.getField("Grey20")value;

      var Grey30=this.getField("Grey30")value;

      var Grey40=this.getField("Grey40")value;

      var White10=this.getField("White10")value;

      var White20=this.getField("White20")value;

      var White30=this.getField("White30")value;

      var White40=this.getField("White40")value;

      var Blue10=this.getField("Blue10")value;

      var Blue20=this.getField("Blue20")value;

      var Blue30=this.getField("Blue30")value;

      var Blue40=this.getField("Blue40")value;

      }

        • 1. Re: document javascripts
          George_Johnson MVP & Adobe Community Professional

          You'd have to do this:

           

          var Red10=this.getField("Red10").value;

           

          In other words, add a "." before "value".

           

          It's not clear to me what this is supposed to do though. When you make the corrections and call the function, it won't do anything. What do you mean by "define the fields"?

          • 2. Re: document javascripts
            davidm76220796 Level 1

            OMG

            I can't believe I missed that.  I fell EXTRA stupid now. (I am stupid enough without the extras.)

             

            What I am trying to accomplish is to avoid having to type all that out (24 times) then do the conditional statement.

            For Example:

             

             

            var Red10=this.getField("Red10").value;

            var Red20=this.getField("Red20").value;

            var Red30=this.getField("Red30").value;

            var Red40=this.getField("Red40").value;

            var Brown10=this.getField("Brown10").value;

            var Brown20=this.getField("Brown20").value;

            var Brown30=this.getField("Brown30").value;

            var Brown40=this.getField("Brown40").value;

            var Grey10=this.getField("Grey10").value;

            var Grey20=this.getField("Grey20").value;

            var Grey30=this.getField("Grey30").value;

            var Grey40=this.getField("Grey40").value;

            var White10=this.getField("White10").value;

            var White20=this.getField("White20").value;

            var White30=this.getField("White30").value;

            var White40=this.getField("White40").value;

            var Blue10=this.getField("Blue10").value;

            var Blue20=this.getField("Blue20").value;

            var Blue30=this.getField("Blue30").value;

            var Blue40=this.getField("Blue40").value;

             

            if((Red10*Red20)<0)

            {

            event.value=Red30

            }

            else

            {

            event.value=Red40

            }

             

            I was hoping I could just do this

             

            AddPressure();

             

            if((Red10*Red20)<0)

            {

            event.value=Red30

            }

            else

            {

            event.value=Red40

            }

            • 3. Re: document javascripts
              George_Johnson MVP & Adobe Community Professional

              You can't do it exactly as you've shown because the variables declared in the function are local to that function and won't be accessible outside of it. What you can do instead is return something when the function gets called, such as an object, like this:

               

              function AddPressure() {

               

                  // Set up an array of field names

                  var aNames = ["Red10", "Red20", "Red30", "Red40",

                  "Brown10", "Brown20", "Brown30", "Brown40",

                  "Grey10", "Grey20", "Grey30", "Grey40",

                  "White10", "White20", "White30", "White40",

                  "Blue10", "Blue20", "Blue30", "Blue40"];

               

               

               

                  // Set up an object that will be returned

                  var oVals = {};

               

                  // Loop through the field names and add name/value pairs to the object

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

                      oVals[aNames[i]] = getField(aNames[i]).value;

                  }

               

                  return oVals;

              }

               

               

               

              Then your other code could be like:

               

              // Get an object containing the name/value pairs

              var oV = AddPressure();

               

              if (oV["Red10"] * oV["Red20"] < 0) {

                  event.value = oV["Red30"];

              } else {

                  event.value = oV["Red40"];

              }

               

               

               

              The other option would be to make Red10, Red20, etc. into variables that have document-global scope, but this isn't the best programming practice. It would look something like this in the document-level JavaScript

               

              // Declare the variables outside of a function so they have a document-global scope

              var Red10, Red20, Red30, Red40, Brown10;  // etc.

               

              function AddPressure() {

               

                  Red10 = getField("Red10").value;

                  // Repeat for other field values

               

              }

               

               

              Since the variables have document-global scope, you can access their values in any other script in the document, like you wanted to originally.

              1 person found this helpful
              • 4. Re: document javascripts
                davidm76220796 Level 1

                George you are AMAZING!!

                 

                Thank You !!!