Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

## Sum of Radio Button Values

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

-Subform2

-Question2

1 Accepted Solution

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

3 Replies

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?

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.