7 Replies Latest reply on Dec 7, 2012 11:24 AM by brainstewn

# Calculate (sum) X if radio button is checked

I Have a form with six columns and 7 rows.  Each row is a question and each column represents a value from 0-5.

If the first column is all checked the value would equal 0 if 4 of the questions are checked in the first column 1 checked in the 2nd column and 1 checked in the 5th column the total should be 6.

Value = 0Value = 1Value = 2Value = 3Value = 4Value = 5
X
X
X
X
X
X
X

TOTAL SHOULD BE 6 in the example above.  If all rows are checked in the 6th column the total would be 35.

Any help in creating a simple script for this form would be greatly appreciated. Thank you in advance.

• ###### 1. Re: Calculate (sum) X if radio button is checked

What are the names of the radio buttons, and what are their export (aka Button Value, Radio Button Choice)?

• ###### 2. Re: Calculate (sum) X if radio button is checked

I have 6 radio buttons on each row

Q1

Q2

Q3

Q4

Q5

Q6

Q7

The choices are named identical to the column they are in.  Column one Q1 has a choice of 1.  Column 5 Q2 has a choice of 5.

Thanks.

• ###### 3. Re: Calculate (sum) X if radio button is checked

OK, you can use something like the following as the custom calculate script for the total field:

// Custom calculate script for Total field

(function () {

// Initialize sum to zero

var sum = 0;

// Loop through the fields to add the value of each row to the sum

for (var i = 1; i < 8; i += 1) {

// Add current row's value to the sum

sum += +getField("Q" + i).value;

}

// Set this field's value to the sum

event.value = sum;

})();

This code assumes that at least one radio button in each row is selected. If it's possible that a radio button in any of the rows is not selected, then the code would have to be adjusted to account for that.

• ###### 4. Re: Calculate (sum) X if radio button is checked

BTW, in this case, you can also use either of the two built-in calculation methods (Value is the: Sum..., Simplified Field Notation). The Simplified Field Notation text would be:

Q1 + Q2 + Q3 + Q4 + Q5 + Q6 + Q7

• ###### 5. Re: Calculate (sum) X if radio button is checked

The first column has a value of zero, the second column has a value of 1 and the last column has a value of 5; in each row.

I'm more eagar than knowledgeable on javascript.  Do I need to add criteria to the script you wrote, cutting and pasting is not working?

How do I give Q1 Choice 4 a value of 4 so that it adds correctly?

I named the field where this script goes to "Total" does that affect the script?

• ###### 6. Re: Calculate (sum) X if radio button is checked

The name of the field that you attached the script to does not matter.

The script gets the value of the radio button group for each row. You set the value of each individual radio button on the Options tab of the Field Properties dialog. From your post #2, its sounds like you did this, so the script should work as-is. Check the JavaScript console (Ctrl+J) to see if any error are being reported. If there are, report back with the exact error message.

• ###### 7. Re: Calculate (sum) X if radio button is checked

The Java error says ?

Acrobat EScript Built-in Functions Version 11.0
Acrobat SOAP 11.0

TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null
TypeError: f is null