10 Replies Latest reply on Mar 29, 2011 8:25 AM by George_Johnson

    Javascript addition

    gwhPoster Level 1

      Hi everyone,

       

      I have about 20 checkboxes in a pdf file. Each of them has an export value assigned to them. Someone may check off say 3 or 4 of the checkboxes and I need the total amount (based on the export value) to be inserted into a field at the bottom of the page. I'm trying to adapt the following code but for some reason the total amount isn't getting inserted into the field:

       

      // Define and Initialize Summation and count variables

      var nEvalSum = 0;

       

      // Acquire the field group

      var oFldGrp = this.getField("CheckBox");

      // Get array of fields in group

      var oFlds = oFldGrp.getArray();

       

      // Loop over Radio Button Groups

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

      {

        // Test field and increment sums if selected

        // Double check to make sure it's a number

        var cFldVal = oFlds[i].value;

        if((cFldVal != "Off") && !isNaN(cFldVal))

        {

           nEvalSum += Number(cFldVal);

        }

      }

       

      // Set the values in the total field

      this.getField("total-amount").value = nEvalSum;

       

      I wondered if someone could help me find out what might be wrong with the code?

       

      Really appreciate any help.

        • 1. Re: Javascript addition
          try67 MVP & Adobe Community Professional

          This script was written for check-boxes with the same name. Is that the case

          in your file?

          If not then you can't use it. You will need to write your own script, or

          have some who know what they're doing do it for you.

          • 2. Re: Javascript addition
            gwhPoster Level 1

            Thanks for the reply,

             

            Yes my checkboxes are all called CheckBox.1, CheckBox.2, CheckBox.3 etc.

             

            Shouldn't that work?

            • 3. Re: Javascript addition
              try67 MVP & Adobe Community Professional

              It should... Check the console for error messages and try adding

              console.println() commands to debug your code.

              • 4. Re: Javascript addition
                try67 MVP & Adobe Community Professional

                I've created a form with 3 fields: CheckBox.1, CheckBox.2 and total-amount

                and placed your code as the custom calculation code for total-amount. It

                works fine.

                • 5. Re: Javascript addition
                  gwhPoster Level 1

                  I just realised that I had to actually click in the total-amount field for the sum to appear. Is there any way for the amount to appear and disappear automatically as the checkboxes are ticked or unticked?

                  • 6. Re: Javascript addition
                    try67 MVP & Adobe Community Professional

                    It should not be necessary to click the field.

                    Are you sure you used the custom calculation script?

                    Are the values of the check-boxes calculated by any other script? If so, you

                    might have a calculation order problem.

                    • 7. Re: Javascript addition
                      gwhPoster Level 1

                      No I was using a javascript action. I changed it to a custom calculation script and it's working well now so thanks very much.

                       

                      Could I ask just one more related question? I'm using the following script on a button so that a message window pops up:

                       

                      var cMessage = "To use the website the following must be installed and/or enabled on your computer:
                      – Macromedia Flash Player plug-in must be installed, (see below)

                      – JavaScript must be enabled, (see below)

                      – One of the following Internet Browsers, (the software you are using to view this page), must be installed:

                      – On a Windows PC - Internet Explorer 6.0 or later or Firefox 1.5 or later";

                       

                      var cMyTitle = "my title";

                       

                      // Display Alert Box with Information Icon

                      app.alert({cMsg:cMessage, cTitle:cMyTitle,

                                 nIcon:3, // Information Icon

                                 nType:0  // Ok Button

                      });

                       

                      The problem is that when I try to exit, I get the error "unterminated string literal".

                       

                      The error goes away if I remove the line breaks, however I need this type of basic formatting in the dialogue box so that it's more readable.

                       

                      Is there a way around the error so that I can keep the formatting?

                      • 8. Re: Javascript addition
                        try67 MVP & Adobe Community Professional

                        A string can't contain literal line-breaks. If you want to include a  line-break in your string you must use an escape character: \n
                        So, for example:
                        "This is the first line.\nThis is the second line.";

                        If you want to actually have line-breaks in your code, to make it more  easily readable to you, you need to end the line with a back-slash. Like  so:
                        "This is the first line.\
                        This is still the first line.\n\
                        Now it's the second line."

                        • 9. Re: Javascript addition
                          gwhPoster Level 1

                          Great - thanks so much again. All solved.

                          • 10. Re: Javascript addition
                            George_Johnson MVP & Adobe Community Professional

                            For a calculate script, this line:

                             

                            this.getField("total-amount").value = nEvalSum;

                             

                            should be changed to:

                             

                            event.value = nEvalSum;