These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculating checked checkboxes

MagpieManny
Registered: Oct 26 2008
Posts: 18

Hey,

I want to calculate the amount of checkboxes checked.

I have for example five checkbox options like:

OOOOO

And I want a field to add the amount of checked options together.

So if I checked 4 the field says 4... when I check 3 , 3 etc

Then I'd also like to ad a number to that amount.. like 3

So when 4 are checked, the field would say 7

,
MM

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The code would depend on how you name your fields. For example, if you named your fields check1, check2, ...check5, the custom calculation script for a text field to display the sum could be:

// Initialize counter variablevar sum = 0; // Loop through the fieldsfor (var i = 1; i < 6; i += 1) {// Add one if check box is not Offif (getField("check" + i).value !== "Off") {sum += 1;}} // Set this field's value to the sumevent.value = sum;

To add a constant, just initialize the sum variable to that number:

var sum = 3;...

George
MagpieManny
Registered: Oct 26 2008
Posts: 18
Nice that also works...

Now:

When I have this: Check1, ,,2, ,,3 and then it goes to ,,20 how do I only calculate those, thus jumping the others from 4 till 19?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You could check each field individually:

// Initialize counter variablevar sum = 0; // Add one if checkbox is not Offif (getField("Check1").value !== "Off") sum += 1;if (getField("Check2").value !== "Off") sum += 1;if (getField("Check3").value !== "Off") sum += 1;if (getField("Check20").value !== "Off") sum += 1; // Set this field's value to the sumevent.value = sum;

George
MagpieManny
Registered: Oct 26 2008
Posts: 18
Nice.. You are really helping me here!

Two last questions to bug you, both are still about check boxes..

Say, for example, I have to rows of checkboxes.

00000 and 00000
In the first row I checked 3, in the other 2.. I want only to add the worst of both...

-----
The other question is..
Can I coun't checkboxes only if a certain dropbox shows a certain value?

I know i't is pretty explicit.. if you can't come up with an answer I understand. You've been a great help so far!
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
For the first, lets say you've calculated the two sums, and that the variables are called sum1 and sum2. To set the value of the field equal the the lowest of the two in a custom calculation script, the last line of the script could be:

// Set the value of this field equal to the lesser of the two sumsevent.value = Math.min(sum1, sum2);

For your final question of the day, the answer is yes. :^)

George
MagpieManny
Registered: Oct 26 2008
Posts: 18
Thanks...

If you happen to be able to explain how to do my final question, that would be great ... No need to rush..

See that weren't any questions!

Thanks for your help George!
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
OK, but I'll have to make a number of assumptions. Let's say the name of the combo box is combo1, that the value it needs to have to activate counting is the string "string1", and that you want the field to be blank when you don't want to count:

// Sum check boxes if combo box has the correct valueif (getField("combo1").value === "string1") { // Add code from above here// that counts checked check boxes } else { // Set the value of this field to nothing (blank)event.value = "";}

George
Cheska
Registered: Jan 23 2009
Posts: 22
I have a similar problem and I am getting frustrated with my average calculations. I hope you can help me with the codes. I have a section with 7 rows of questions. Each row has 4 checkboxes and all 4 checkboxes have the same field name (ex. cbSec1A1 for 1st row, followed by cbSec1A2 for 2nd row, and so on) All 4 checkboxes in a row are set to global, so each row can only take 1 check at a time. "On Values" are from 1 to 4, "Off Value" is zero. I created a numeric field to handle and display the calculation named nfRatingSec1A. The problem is: there will be rows that will be left unanswered or i should say no check in a row. It is easy to get the sum of all the checkboxes but my problem is the denominator which is a variable = to the count of checks (On Values). I have done a lot of research on javascript (for loop) and tried them with acrobat but nothing seem to work or I was not doing it right.

I will appreciate all the help. Thanks in advance.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
I'm assuming you're using using LiveCycle Designer to create your form. In your loop you will have to set up a variable to determine the denominator. If the value of the check box for a particular row is not zero, then increment the denominator counter. If you post some code, we could suggest some changes.

George
Cheska
Registered: Jan 23 2009
Posts: 22
Thank you for replying so quickly, George. This is actually my first form project with acrobat. I am using Acrobat Professional 7.0 and I took off from there to create a form. It opened another area where it's called a designer so that must be the LiveCycle? A developer suggested that I use this following javascript:

// Initialize counter variable
var isum = 0;
var icount = 0;

// Loop through the fields
for (var i = 1; i < 6; i += 1) {
// Add one if check box is not Off
if (getField("cbSec1A" + i).value !== "Off") {
isum += getField("cbSec1A" + i).value;
icount += 1;
}
}

// Set our formula's value to the sum and count
nfRatingSec1A = isum / icount;

I pasted this whole thing in a box on a numeric field named nfRatingSec1A and set to calculate in javascript. (I hope you understand what I meant by these settings). But this did not work.

Originally, I use FormCalc for my calculation and I used the following formula:

nfRatingSec1A = sum (cbSec1A1,cbSec1A2,cbSec1A3,cbSec1A4,cbSec1A5,cbSec1A6,cbSec1A7) / count(cbSec1A1,cbSec1A2,cbSec1A3,cbSec1A4,cbSec1A5,cbSec1A6,cbSec1A7)

This is working fine until I encountered rows 6 and 7 not used and threw off my average calculation because denominator is no longer 7.

So the logic I want acrobat to calculate for me is:

"the sum of the values of all checked boxes / the count of checkboxes which values are not equal to 0 or checkboxes which values are "ON"."

I am also not sure if numeric field is the right field to handle the javascript calculation. Should I try text field instead?

I guess I need to go to a class :) My boss will be glad to have this form by next week though. I hope you can help me.

Cheska
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
That first bit of code looks like something I would have written (except for that last line), but it is for use with a form that you create with Acrobat, as opposed to LiveCycle Designer. Are you using Designer for a reason, or did you just stumble upon it? The last line should be:

event.value = isum / icount;
but again, it won't work in a Designer created form.

Regarding which type of form you should be using (Acroform created in Acrobat or XFA form created in LiveCycle Designer), if you provide more background (purpose of form, who the users are, what version of Acrobat (Reader) it needs to work with, etc.), we may be able to advise you better.

If you decide to stick with Designer, you might want to post questions in the Designer forum here.

George
Cheska
Registered: Jan 23 2009
Posts: 22
Well, because of my ignorance, I thought designer was the only place where to create the form. This is my first time and no one at work seems familiar with this app anyway. It appears to me I can create the form somewhere in acrobat and could successfully use those formula there. I will try to rediscover acrobat and re-create the form there and we'll see if I can successfully find my way out.

By the way, before I proceed, can I import or fix this form in acrobat, or should I start from scratch?

I am very grateful for your advices and help.

Cheska
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
If you want to use Acrobat (and that is what I would advise), you should start from scratch. You should first create a PDF that has the underlying form layout, and add the form fields using the various form tools in Acrobat. There are a number of resources here that will help you get started, and Acrobat's help should be useful as well.

George
Cheska
Registered: Jan 23 2009
Posts: 22
Guess what? I find it easier to work with designer than the acrobat so I will try asking someone in the designer forum first. If I can't get any help there then I have no choice but to re-create. What I find so odd (or maybe out of ignorance again), I cannot start a new form in acrobat, I have to create a pdf from another file??? I tried to create from a word file but acrobat does not recognize word 2007??? Everything is just strange. I have worked with Access db forms/reports and Crystal reports but did not go through all these issues.

Anyway, thanks for being so patient with me. I'll surely let you know if I ever resolve this issue.

Cheska