• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How does one sum a specific choice from a series of groups of radio buttons?

New Here ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

I have a list of about 25 questions.  They are radio buttons with the choice of Yes, No, Maybe.

Fields are set up as:

Q1 is set up as  Group1 (Choice1"Yes", Chioce2"No", Choice3"Maybe")

Q2 is set up as Group2 (Choice1"Yes", Chioce2"No", Choice3"Maybe")

All the way to Q25

Im trying to add all of the Yes (or choice1) answers in one field, all of the No (of choice2) answers another field and all of the Maybe (choice3) answers in another field.

When i try to calculate using the sum function i'm only able to choose groups and dont have the option to specify the choice fields.

Does anyone have any sugestions on how to add up the specific choice fields?  would be very much appreciated!!!

TOPICS
JavaScript , PDF forms

Views

865

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 20, 2021 Apr 20, 2021

Lets say your 3 fields to show sum are called 'Yes', 'No' and 'Maybe', use this script as calculation script of 'Maybe' field:

var y = 0;
var n = 0;
var m = 0;
for ( var i=1; i<=25; i++){
if(this.getField("Group"+i).value == "Choice1")y++;
if(this.getField("Group"+i).value == "Choice2")n++;
if(this.getField("Group"+i).value == "Choice3")m++;
if(y == 0 && n == 0 && m == 0){
this.getField("Yes").value = "";
this.getField("No").value = "";
event.value = "";}
else{
this.getField("Yes").value = y;
this.getField("

...

Votes

Translate

Translate
Community Expert ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Lets say your 3 fields to show sum are called 'Yes', 'No' and 'Maybe', use this script as calculation script of 'Maybe' field:

var y = 0;
var n = 0;
var m = 0;
for ( var i=1; i<=25; i++){
if(this.getField("Group"+i).value == "Choice1")y++;
if(this.getField("Group"+i).value == "Choice2")n++;
if(this.getField("Group"+i).value == "Choice3")m++;
if(y == 0 && n == 0 && m == 0){
this.getField("Yes").value = "";
this.getField("No").value = "";
event.value = "";}
else{
this.getField("Yes").value = y;
this.getField("No").value = n;
event.value = m;}}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Thanks Nesa. I put that script in but its not calculating.   Im at a loss as to why

Screen Shot 2021-04-20 at 3.11.05 PM.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Check javascript for error. Check that all your fields are named correctly and that they have correct choices, if that doesn't help share your file so I can see whats going on.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Script and field names seem fine to me...but i'm also the one who came onto the forum so its not saying much!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Your fields are named 'Group' not 'Q', so in script change "Q" with "Group" it was my mistake I thought your fields are named Q.

EDIT: I fixed code above.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 20, 2021 Apr 20, 2021

Copy link to clipboard

Copied

Works like a charm!  Thanks for yoru help Nesa!!!  great appreciated

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 07, 2022 Apr 07, 2022

Copy link to clipboard

Copied

I'm trying to do something similar but with only 2 groups, yes and no.

 

When I label the groups, do I need to assign each group a Yes or No or will this still work with only using "choice1" or "choice2"?

 

What would a script look like for only 2 options? I removed the what i though needed to be removed from the above formula but nothing is happening.  I'm not sure if Y & N are coming from the form or if we are just adding them

var y = 0;
var n = 0;
for ( var i=1; i<=25; i++){
if(this.getField("Group"+i).value == "Choice1")y++;
if(this.getField("Group"+i).value == "Choice2")n++;
if(y == 0 && n == 0){
this.getField("Yes").value = "";
this.getField("No").value = "";
event.value = "";}
else{
this.getField("No").value = n;
event.value = y;}}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 07, 2022 Apr 07, 2022

Copy link to clipboard

Copied

LATEST

Where did you put script?

You also need to change '25' to number of your fields.

You can leave "Choice1" and "Choice2" also it's case sensitive so 'choice1' and 'Choice1' is not same.

EDIT:

If you have fields "Yes" and "No" use script in one of those fields as 'Custom calculation script', make sure your groups are named in unison "Group1","Group2","Group3"...etc, replace red X with number of your groups:

var y = 0;
var n = 0;
for ( var i=1; i<=X; i++){
if(this.getField("Group"+i).value == "Choice1")y++;
if(this.getField("Group"+i).value == "Choice2")n++;}
this.getField("Yes").value = y == 0 ? "" : y;
this.getField("No").value = n == 0 ? "" : n;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines