• How does one sum a specific choice from a series o...

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

New Here ,
Apr 20, 2021 Apr 20, 2021

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

218

Likes

Report

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

Adobe Community Professional , 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("

...

Likes

8 Replies 8
Apr 20, 2021 Apr 20, 2021

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;}}

Likes

Report

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

Copied

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

Likes

Report

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

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.

Likes

Report

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

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!!

Likes

Report

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

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.

Likes

Report

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

Copied

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

Likes

Report

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

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;}}

Likes

Report

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

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;

Likes

Report

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