5 Replies Latest reply on Oct 28, 2010 1:04 AM by mrs daniels

    Count checkboxes

    mrs daniels

      I need a javascript to count a check box.  My form contains a field called mac stat 1.0 thru mac stat 1.9 -- I need to count how many are checked.

       

      I have tried several codes but I can't seem to get them to work.  I think they may actually be for multiple checkboxes.  Not that familiar with javascript and figuring out where to insert my field names.

       

      thanks.

        • 1. Re: Count checkboxes
          gkaiseril MVP & Adobe Community Professional

          Check Boxes and Radio Buttons have a field value of 'Off' when no single Check Box or Radio Button is selected. It is possible to have more than one Check Box of Radio Button with the same name, but each individual field (widget) should have a unique export value. When a series of Check Boxes and Radio Buttons grouped by name with a unique export value they are called an 'exclusionary' group and only one of the members of the group can be selected. selecting another box or button, will cause a previously selected item to be unchecked. With Check Boxes selecting a checked Check Box will unchecked that box. Once a Radio Button in a group has been selected, there must always be a selected Radio Button.

           

          To count Check Boxes or Radio Buttons that have a selection made, one only needs to count the fields that do not have a value of 'Off'.

           

          The following function can be placed as a document level function and called as often as needed with different field names to count selected boxes or buttons.

           

          function CountCheckBoxes(aFIeldNames) {

          // count field names that have been selected

          var count = 0;

          // loop through array of field names

          for(i = 0; i < aFieldNames.length; i++) {

             // for field names with a value of not Off increment counter

             if(this.getFieldName(aFieldNames[i]).value != "Off") count++;

          } // end loop of field names

          // return count

          return count;

          } // end CountCheckBoxes

           

          A custom calculation script like the following will count the provided fields that have been selected.

           

          // var define field names to be tested

          var aFields = new Array('mac stat 1.0', 'mac stat 1.1', 'mac stat 1.2', 'mac stat 1.3', 'mac stat 1.4', 'mac stat 1.5', 'mac stat 1.6', 'mac stat 1.7',

          'mac stat 1.8', 'mac stat 1.9');

          // count field names that have been selected

          event.value = CountCheckBoxes(aFields);

          • 2. Re: Count checkboxes
            mrs daniels Level 1

            I copied  the whole section from "function CountCheckBoxes(aFieldNames) {"

            including the calculation script but it didn't work.

             

            I got this error:  TypeError: this.getFieldName is not a function

            7:Field:Calculate

             

            I am very new to scripting.  Am I suppose to replace any of the names with

            my form names?

             

            I've been looking at different websites and I can't find anything that seems

            particular to my form.

             

            I basically want a script that says will go through the document and

            anywhere that the field  "Mac Stat x.x" = "true" or "ON" then count and

            display the number in the box that I put this formula.

             

            It seemed so easy with the radial buttons.  Or at least it made sense.

             

            So I don't want the script to search all checkboxes in the document -- just

            Mac Stat boxes.

             

            Brenda

             

            (please don't give up on me -- I'm trying).

            • 3. Re: Count checkboxes
              try67 MVP & Adobe Community Professional

              The correct name of this function is getNthFieldName

              • 4. Re: Count checkboxes
                gkaiseril MVP & Adobe Community Professional

                The document level function should be:

                 

                function CountCheckBoxes(aFieldNames) {
                // count field names that have been selected
                var count = 0;
                // loop through array of field names
                for(i = 0; i < aFieldNames.length; i++) {
                   // for field names with a value of not Off increment counter
                   if(this.getField(aFieldNames[i]).value != "Off") count++;
                } // end loop of field names
                // return count
                return count;
                } // end CountCheckBoxes

                 

                And what are the export value for your check boxes?

                 

                If a check box is not selected the value is "Off", if a check box is selected then the value is the export value for tat  check box.

                • 5. Re: Count checkboxes
                  mrs daniels Level 1

                  Yeah it worked!!!  Thank you so much.

                   

                  In the new array section, mac stat 1.0 was the name of my fields.

                  So just change this to your field names and hopefully it works if anyone is

                  looking for a similar code.

                   

                   

                   

                  The following is what I used:

                   

                  function CountCheckBoxes(aFieldNames) {

                  // count field names that have been selected

                  var count = 0;

                  var cField = "";

                  // loop through array of field names

                  for(i = 0; i < aFieldNames.length; i++) {

                     // for field names with a value of not Off increment counter

                     if(this.getField(aFieldNames[i]).value != "Off") count++;

                  } // end loop of field names

                  // return count

                  return count;

                  } // end CountCheckBoxes

                   

                  // var define field names to be tested

                  var aFields = new Array('mac stat 1.0', 'mac stat 1.1', 'mac stat 1.2', 'mac

                  stat 1.3', 'mac stat 1.4', 'mac stat 1.5', 'mac stat 1.6', 'mac stat 1.7',

                  'mac stat 1.8', 'mac stat 1.9');

                  // count field names that have been selected

                  event.value = CountCheckBoxes(aFields);

                   

                   

                  Thanks again for the help - Brenda