3 Replies Latest reply on Mar 1, 2011 1:18 PM by Kevin Cavallo

    Sum of Radio Button Values

    Josh_B_369 Level 1

      Hi,

       

      I'm looking to add the values of separate radio buttons together. (JavaScript knowledge = minimal)

       

      If I have one group of radio buttons with 5 different answers (each answer with a value 1-5). Then I have another group of radio buttons with 2 different answers (each answer with a value 1-2).

       

      From these two groups of radio buttons I want to have a numeric field that calculates the sum of the two groups of radio buttons. (this would range from a total value of 2-7)

       

      Take for instance the following example which would be shown in the Hierarchy Pallette where the numbers represent the value.

           -Page1

                -Subform1

                     -Question1

                          -Radio_Button_A = 1

                          -Radio_Button_B = 2

                          -Radio_Button_C = 3

                          -Radio_Button_D = 4

                          -Radio_Button_E = 5

                -Subform2

                     -Question2

                          -Radio_Button_A = 1

                          -Radio_Button_B = 2

        • 1. Re: Sum of Radio Button Values
          Josh_B_369 Level 1

          I found the following solution using FormCalc but would rather use the JavaScript equivilant.

           

          sum(Subform1.Question1, Subform2.Question2)

           

          Is there a Javascript script equal to the FormCalc script above?

          • 2. Re: Sum of Radio Button Values
            Kevin Cavallo Level 4

                 var result = parseInt(Subform1.Question1.rawValue) + parseInt(Subform2.Question2.rawValue);

             

            Since the values returned from the radio button groups are strings, you need to convert the values to an integer using parseInt() before you can add them.

             

            However: be careful where you are doing the calculation if you are not defaulting a value for the button groups.  If none of the buttons are selected in a group, it will return an empty string for the value, which parseInt will convert to "NaN" (meaning Not a Number) and you will end up with a meaningless result.  Either: have a default for all button groups, or check that a valid number exists for each group before doing the calculation, or put the calculation in an event that will occur after all selections have been made.

            • 3. Re: Sum of Radio Button Values
              Kevin Cavallo Level 4

              Sorry, read your initial post again more closely.  If you're going to use a numeric field to display the sum, then just put the following in the calculate event of the numeric field:

               

              parseInt(Subform1.Question1.rawValue)

              + parseInt(Subform2.Question2.rawValue);

               

              The numeric field will throw away any value that isn't a number, so it won't display anything until you actually have two valid selections.

               

              Kevin