Expand my Community achievements bar.

SOLVED

Sum of Radio Button Values

Avatar

Former Community Member

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 Accepted Solution

Avatar

Correct answer by
Level 6

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

View solution in original post

3 Replies

Avatar

Former Community Member

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?

Avatar

Level 6

     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.

Avatar

Correct answer by
Level 6

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