3 Replies Latest reply on Mar 14, 2012 8:59 PM by George_Johnson

    Script for automatically calculating responses to Radio Button questions?

    star_cookie_queen

      Hello,

       

      Does anyone know if its possible / how to write a script for autmatically counting responses from radio buttons?

       

      As an example, I have a form with 14questions, where the answers are either 'Yes" "Unknown" and "No" - I have used radio buttons for each question.

      At the bottom of the form, I'd like to have it automatically calcuate how many "yes" "unknown" and "No" responses there was. (ie. a text box which displays the count for "yes", another text box that displays the count for "No" ..etc)

       

      Can anyone help me with that?

       

       

      Thank you so much

        • 1. Re: Script for automatically calculating responses to Radio Button questions?
          George_Johnson MVP & Adobe Community Professional

          Sure it's possible. If you want help with the specific code, you'll need to tell us what field names you're using for the radio buttons and what you've set up for the "Button Value" of each.

          1 person found this helpful
          • 2. Re: Script for automatically calculating responses to Radio Button questions?
            star_cookie_queen Level 1

            Thanks George, that would be VERY helpful!

             

            To try and keep it easy, I'll name each question (so each group of radio buttons) Question 1, Question 2, etc all the way through to Question 14.

            As for the button name of each, they are also quite simple - just "Yes" "Unknown" and "No"

             

            Thanks so much!

            • 3. Re: Script for automatically calculating responses to Radio Button questions?
              George_Johnson MVP & Adobe Community Professional

              OK, for the text field that counts the number radio button groups that have the Yes option selected, you can use code like:

               

              // Custom Calculate script for text field
              (function () {
              
                  // Initialize counter
                  var sum = 0;
              
                  // Loop through the fields
                  // and increment the counter for each Yes value
                  for (var i = 1; i < 15; i += 1) {
                      if (getField("Question " + i).value === "Yes") {
                          sum += 1;
                      }
                  }
              
                  // Set the value of this field to the sum
                  event.value = sum;
              
              })();
              

               

              Do the same for the other fields, but change "Yes" in the code above to the appropriate string, either "No" or "Unknown".

               

              It would make sense to convert this to a function in a document-level script, something like:

               

              // Document-level function
              var radioButtonValueCount = function (sVal) {
              
                  // Initialize counter
                  var sum = 0;
              
                  // Loop through the fields
                  // and increment the counter for each sVal value
                  for (var i = 1; i < 15; i += 1) {
                      if (getField("Question " + i).value === sVal) {
                          sum += 1;
                      }
                  }
              
                  // Set the value of the field that called this function to the sum
                  event.value = sum;
              
              }
              

               

               

              You would then call it like this in the calculate event of a text field that counts the number of Yes selections:

               

              // Set this field to the number of Yes selections
              radioButtonValueCount("Yes");
              

               

              Do the same thing for the No and Unknown fields.