4 Replies Latest reply on Sep 19, 2013 10:18 AM by ab4

    JavaScript for multiple checkbox types

    ab4

      Help please! I have used the javascript listed below (successfully) to count a number of checkboxes in my document that were named AM1.1, AM1.2, AM1.3, etc, but now I am trying to count multiple checkboxes in the same way but two different lists. So, for example, I want the javascript to count AM1.1, AM1.2, AM1.3, etc AND count PM1.1, PM1.2, PM1.3, etc to add up to one value. I can't figure out how to add the lines needed to this script to add both field groups. Any advice?

       

      Script I am currently using successfully to just count the AM1 checkboxes:

       

      // Custom calculate script

      (function () {

       

          // Get an array of the bed fields

          var fa = getField("AM1").getArray();

       

          // Initialize counter

          var sum = 0;

       

          // Loop through the fields and update counter

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

              sum += fa[i].value !== "Off" ? 1 : 0;

          }

       

          // Set this field's value to the sum

          event.value = sum;

       

      })();

        • 1. Re: JavaScript for multiple checkbox types
          MichaelN Level 3

          This works:

           

               // Custom calculate script
               (function () {

           

              // Get an array of the bed fields
              var fa = getField("AM1").getArray();
              var fp = getField("PM1").getArray();

              // Initialize counter
              var sum1 = 0;

           

              // Loop through the fields and update counter
              for (var i = 0; i < fa.length; i += 1) {
                  sum1 += fa[i].value !== "Off" ? 1 : 0;
              }

           

              var sum2 = 0;
              // Loop through the fields and update counter
              for (var i = 0; i < fp.length; i += 1) {
                  sum2 += fp[i].value !== "Off" ? 1 : 0;
              }

              // Set this field's value to the sum
              event.value = sum1 + sum2;

           

               })();

          1 person found this helpful
          • 2. Re: JavaScript for multiple checkbox types
            ab4 Level 1

            Hi Michael, thank you so much for your post. I plugged in this exact code though, and my total is still just showing zero in that field when I check the checkboxes for AM1.1, AM1.2, PM1.1, etc. Any ideas why it might not be working? Sorry in advance — I am a javascript novice (at best).

            • 3. Re: JavaScript for multiple checkbox types
              MichaelN Level 3

              In my test file that works, the AM and PM check boxes have an export value of "Yes".

               

              In my file they are called "AM1.0", "AM1.1", "AM1.2" etc, and "PM1.0", "PM1.1", "PM1.2", etc.

               

              Also, here is the script cleaned up a bit:

               

                  // Custom calculate script

               

                  // Get an array of the bed fields
                  var fa = getField("AM1").getArray();
                  var fp = getField("PM1").getArray();

                  // Initialize counter
                  var sum1 = 0;

               

                  // Loop through the fields and update counter
                  for (var i = 0; i < fa.length; i += 1) {
                      sum1 += fa[i].value !== "Off" ? 1 : 0;
                  }

               

                  var sum2 = 0;
                  // Loop through the fields and update counter
                  for (var i = 0; i < fp.length; i += 1) {
                      sum2 += fp[i].value !== "Off" ? 1 : 0;
                  }

                  // Set this field's value to the sum
                  event.value = sum1 + sum2;

               

               

              Check the names of all the check boxes.

               

              Also, open the JavaScript Console and see if an error message appears, as this may help pinpoint the problem. If there is an error message, post it here.

               

              If it still doesn't work, post your code here and I will try to help.

              • 4. Re: JavaScript for multiple checkbox types
                ab4 Level 1

                I'm not sure why that first one didn't work, but the last one you posted did! Thank you so much! So excited for this to work. Appreciate your work and quick responses.