# Select Radio Button fields summed on same form into a read only fields

**URL:** <https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [May 9, 2023, 12:04pm UTC](https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090 "2023-05-09T12:04:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tina-ross](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tina-ross](https://forum.wixstudio.com/u/tina-ross)\
**Post date:** [May 9, 2023, 12:04pm UTC](https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090/1 "2023-05-09T12:04:17Z")

</div>

I have a strengths assessment with 55 statements that people rate on a 0-4 scale for each statement. I am looking for a way at the bottom of the form for the users to see live sums of their strengths based on how they answered certain statements.

I essentially would need something like the below with sums of radiogroups into read only text fields:

Leadership - sum of radiogroup1, radiogroup5, radiogroup12, radiogroup25, & radiogroup46

Service - sum of radiogroup3, radiogroup19, radiogroup27, radiogroup33, & radiogroup49

Teaching - sum of radiogroup7, radiogroup18, radiogroup22, radiogroup31, & radiogroup44

AND SO ON…

I am a novice/dabbler coder who is married to a software developer but he doesn’t know/use wix for any of his websites or business so he suggested I direction first from the community.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [May 10, 2023, 10:47am UTC](https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090/2 "2023-05-10T10:47:09Z")

</div>

1. First question, if you only need single-selection options?  
If so, then —\> a Radio-Button-Group is the right choice.  
If you also have multi-optional choice-options, then you will need → Check-Box-Groups.

2. What is the difference between a ordinary Radio-Button and a Radio-Button-Group?  
Yes, exactly! In a group you have several results and on a single R-Button you just have one.

Le’s take a look onto an example…

```javascript
$w("#myRadioGroup").options = [
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

```

As we can see, here we have an → ARRAY\<-- including —\> 3-Objects \<----.

ARRAY = [1, 2, 3, 4, …]  
OBJECT = {firstName: "Tina, lastName: “Ross”},

Let’s say you now want to know which of your options were selected…  
That means we have to get the value(s) of our Radio-Button-Group.

```javascript
let myValue =$w("#myRadioGroup").value;

```

RBG = Radio-Button-Group

```javascript
$w.onReady(()=>{
	let myRBG_Value =$w("#myRadioGroup").value;
	console.log("RBG-Value: ", myRBGValue);
}); 

```

Now you already must see the RESULTS of your choices.  
But wait you didn’t do any choices, yet !!!

You want to get result after you have changed the OPTIONS/VALUES inside of your RBG.  
Let’s try it again…

```javascript
$w.onReady(()=>{
  $w("#myRadioGroup").onChange(()=>{
	let myRBG_Values = $w("#myRadioGroup").value;
	console.log("RBG-Value: ", myRBGValue);
  });
}); 

```

Now you get all the selected options inside your CONSOLE, when you change value(s) of your RBG.

As we already know, we get an ARRAY, which includes all the selected OBJECTS.  
And the OBJECT itself includes —\> LABEL \<— and VALUE.

```javascript
{"label": "Who's on first!", "value": "first"},

```

How to get the first value of all included objects?

```javascript
let myFirstValue = myRBG_Values[0].value;
console.log("First-Value: ", myFirstValue);

```

How to get the first label of all included objects?

```javascript
let myFirstLabel = myRBG_Values[0].label;
console.log("First-Label: ", myFirstLabel);

```

And the rest will be just some calculations…

---

<div class="post-metadata">

**Author:** ![tina-ross](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tina-ross](https://forum.wixstudio.com/u/tina-ross)\
**Post date:** [May 10, 2023, 3:08pm UTC](https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090/3 "2023-05-10T15:08:03Z")

</div>

Thanks for your reply but I’m not certain I fully understand what you are saying and also if you are understanding what I am needing help with. Here is a link to an example of a form that I would like to move to our Wix website if possible from Formstack Forms: [https://allshoreswesleyanchurch.formstack.com/forms/assessment](https://allshoreswesleyanchurch.formstack.com/forms/assessment)

The answers are already numerical and as you answer the results near the bottom change.

That is what I’m looking for!

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [May 10, 2023, 7:24pm UTC](https://forum.wixstudio.com/t/select-radio-button-fields-summed-on-same-form-into-a-read-only-fields/11090/4 "2023-05-10T19:24:00Z")

</div>

Ok, understood.

Let’s say, you have 12 RBGs (12 questions) with 4-options each, representing 4-types of answers. 3-Questions for each of a TYPE.

Each of the 4-possible values (consisting of number 1-4), represent the category where you want to calculate (adding) count-up a number.

The IDs of your Radio-Button-Groups will be…  
rbg1  
rbg2  
rbg3  
rbg4  
…  
…  
rbg12

But wait! Isn’t there even a better idea out there?  
What about directly deviding your RBGs into different types of a group ?

For example…

rbg1Type2  
rbg2Type1  
rbg3Type4  
rbg4Type3  
rbg5Type2  
rbg6Type1  
rbg7Type3  
rbg8Type4  
rbg9Type3  
rbg10Type4  
rbg11Type1  
rbg12Type2

Ok, but why do i need this? Let’s sort our created RBG-Types…  
**Type-1** —\> [**rbg2** , **rbg6** , **rbg11**] \<— **typeArray1**  
**Type-2** —\> [rbg1, rbg5, rbg12] \<— **typeArray2**  
**Type-3** → [rbg4, rbg7, rbg9] \<— **typeArray3**  
**Type-4** → [rbg3, rbg8, rbg10] \<— **typeArray4**

What for do i need the definition of types now? ? ?  
Well, the types will represent …(here are your 4-types)…

 ![](https://us1.discourse-cdn.com/wix/original/3X/0/3/039a04c8685888fb9e848685cc0ea7dcb5293547.png)

Now you will need a LOOP, to loop trough the **TYPE-ARRAY** to be able to calculate all data for each **type**.

Let’s say → **TYPE-1** = " **LEADERSHIP**"

Now someone has chosen options in **rbg2** , **rbg6** and **rbg11**

**Of course you want now to get the end-result for**  **TYPE-1**  **.**  
**How high will be the score for type-1 (LEADERSHIP) ???**

**Let’s do a loop, to find out which score we get for —\> LEADERSHIP (TYPE-1)…**

**We will save the end-result for the TAPE-1-LOOP in a variable called (** endResultType1 **)…**

let endResultType1 ;

**typeArray1**.forEach(element=\> {  
endResultType1 = endResultType1 + element.value;  
});

This will loop through all (3 selected values of —\> **rbg2** , **rbg6** , **rbg11** \<— and sum them (save them) inside the —\> endResultType1 \<— variable.  
After the loop has finished it’s work → your first result of your related to your first-type-questions has been calculated. Now you know the score for → LEADERSHIP

Doing the same 3-more times for the other TYPES, you will get at least 4 different scores…

SCORE-1 (LEADERSHIP) —\> for example 12  
SCORE-2 (HELP) --------------\> for example 6  
SCORE-3 (HOSPITALITY) → for example 4  
SCORE-4 (SERVICE) ---------\> for example 8

Now you got all your 4-scores you wanted to have.  
What next? Find the highest score?

And again you can use a loop, if you convert your fixed result-variables into an ARRAY aswell…

How to do ?

**CHANGING our previous code…**

instead of → let endResultType1;  
we use now ----\> let endResults = ; \<— defining our endResults as an ARRAY.

Our resulting CODE would be…

```javascript
let endResults = [];

typeArray1.forEach(element=> {
    endResults[0] = endResultType1 + element.value;
});

typeArray2.forEach(element=> {
    endResults[1] = endResultType2 + element.value;
});

typeArray3.forEach(element=> {
    endResults[2] = endResultType3 + element.value;
});

typeArray4.forEach(element=> {
    endResults[3] = endResultType4 + element.value;
});

```

Let’s generate the whole code, to solve your issue…

We already learned…

```javascript
$w.onReady(()=>{
    
}); 

```

Going step further and adding the new knowledge… defining the TYPE-arrays.  
Since we are using now type-arrays, we don’t need to generate a type-specific element-ID like shown above --\>(rbg9Type3)\<-- anymore!  
Because now we have our Type-Arrays, where we simply add the IDs of the RBGs, corresponding to the right type (questions).

```javascript
$w.onReady(()=>{
    //Type-1 ---> 
    let typeArray1 = [rbg2, rbg6, rbg11]; //for LEADERSHIP

    //Type-2--->
    let typeArray2 = [rbg1, rbg5, rbg12]; //for HELP
    
    //Type-3 -->
    let typeArray3 = [rbg4, rbg7, rbg9]; //for HOSPITALITY
    
    //Type-4 -->
    let typeArray4 = [rbg3, rbg8, rbg10]; // for SERVICES
}); 

```

Going step further…

```javascript
$w.onReady(()=>{
    let endResults = [];
    //--------------------------------------
    let typeArray1 = [rbg2, rbg6, rbg11];
    let typeArray2 = [rbg1, rbg5, rbg12];
    let typeArray3 = [rbg4, rbg7, rbg9];
    let typeArray4 = [rbg3, rbg8, rbg10];
    //--------------------------------------
     typeArray1.forEach(element=> {
        endResults[0] = endResults[0] + element.value;
    });

    typeArray2.forEach(element=> {
        endResults[1] = endResults[1] + element.value;
    });

    typeArray3.forEach(element=> {
        endResults[2] = endResults[2] + element.value;
    });

    typeArray4.forEach(element=> {
        endResults[3] = endResults[3] + element.value;
    });
}); 

```

Of course the shown code is still not complete, what else did we forgot or did not add into our code? Yes, sure, all our RBGs are still not completely defined, let’s do it…

```javascript
$w.onReady(()=>{
    let endResults = [];
    //-------------------------------------
    let rbg1 = $w('#rbg1'); //--> ID of Radio-Button-Group 
    let rbg2 = $w('#rbg2');
    let rbg3 = $w('#rbg3');
    let rbg4 = $w('#rbg4');
    let rbg5 = $w('#rbg5');
    let rbg6 = $w('#rbg6');
    let rbg7 = $w('#rbg7');
    let rbg8 = $w('#rbg8');
    let rbg9 = $w('#rbg9');
    let rbg10 = $w('#rbg10');
    let rbg11 = $w('#rbg11');
    let rbg12 = $w('#rbg12');
    //--------------------------------------
    let typeArray1 = [rbg2, rbg6, rbg11];
    let typeArray2 = [rbg1, rbg5, rbg12];
    let typeArray3 = [rbg4, rbg7, rbg9];
    let typeArray4 = [rbg3, rbg8, rbg10];
    //--------------------------------------
    typeArray1.forEach(element=> {
        endResults[0] = endResults[0] + element.value;
    });

    typeArray2.forEach(element=> {
        endResults[1] = endResults[1] + element.value;
    });

    typeArray3.forEach(element=> {
        endResults[2] = endResults[2] + element.value;
    });

    typeArray4.forEach(element=> {
        endResults[3] = endResults[3] + element.value;
    });
}); 

```

Now we already getting closer to our wished END-RESULT !!! 😁😉

What else do you need? What else you already have learned in my last post-answer?

-What about → a button-click, which starts all the process???  
-What about → adding some → console-logs?

And do not forget to add 12x RBGs onto your page with the corresponding IDs, like shown inside of the code.

If you go even further, you can shorten this code by 50% , but this would be next level 😁.

If your friend is a coder → he will understand. 😁.

Try to complete this code, by adding the last step(s) to get your wished function.

![Code-Ninja](https://us1.discourse-cdn.com/wix/original/3X/2/b/2bb36ac81f86fab0361eff67160162b408cf26ce.gif)
