4 Replies Latest reply on Aug 14, 2016 3:34 PM by try67

    adding value (0 or 1) to check boxes, and adding them

    davegirr

      Is there a way to automatically add up the number of boxes that are checked? For example, if there are 20 check boxes, is there a way to create a script that counts the number of boxes checked? So if 13 boxes are checked, it will say 13, if 17 are checked, it will say 17, etc...? If you could reply, I'd be very grateful. Thank you.

      Also, I found out how to make the text box in the "Total Points" be the product of "Total Items" and "Points Per Item", so to make the below form completely automatic, I just need the "Total Items" box to add up each check box in its column.

       

      DRAW.jpg

        • 1. Re: adding value (0 or 1) to check boxes, and adding them
          try67 MVP & Adobe Community Professional

          Yes, it can be done. This function will return the number of checked check-boxes based on the array of names you provide to it:

           

          function countCheckBoxes(fields) {
              var total = 0;
              for (var i in fields) {
                  var f = this.getField(fields[i]);
                  if (f==null) {
                      console.println("Can't find: " + fields[i]);
                      continue;
                  }
                  if (f.value!="Off")
                      total++;
              }
              return total;
          }
          

           

          So you can call it like this from the custom calculation script of a text field to populate it with the number of checked boxes:

          event.value = countCheckBoxes(["Box1", "Box2", "Box3", "Box4"]);
          
          • 2. Re: adding value (0 or 1) to check boxes, and adding them
            davegirr Level 1

            Thank you, this worked:

             

            event.value = countCheckBoxes(["11", "12", "13", "14", "15", "16", "17", "18", "19", "111", "112", "113", "114", "115", "116", "117", "118", "119"])

             

            It is now counting perfectly, however I'm noticing that it isn't auto-updating constantly like Excel does, so it is off by one count every time. Is there any way to make the rules update every few seconds or so?

            • 3. Re: adding value (0 or 1) to check boxes, and adding them
              davegirr Level 1

              Or, better yet, can you assign the value of the check box to be a set number? Like 2 or 3 instead of 1?

              • 4. Re: adding value (0 or 1) to check boxes, and adding them
                try67 MVP & Adobe Community Professional

                Are you using a script to set the values of these check-boxes, or are they ticked manually? If the former then you need to check the field calculation order and make sure that that script executes before the calculation of the text field.

                 

                Yes, it's possible to apply different values to each check-box. If you do that then you have to do it to all of them, though. Even if their value is just 1. Once you've done that replaces lines 9-10 in the code above with this:

                 

                if (f.valueAsString!="Off") 
                    total+=Number(f.value);