How to Count Radio Button Choices in Acrobat JavaScript

A question that comes up every now and then on either AcrobatUsers.com or in the Acrobat JavaScript Forum is about how to count how many radio buttons with a certain value were selected by a user. Let’s for example say you have a survey that has 10 yes/no options, and you want to know how often a user selected “yes”, you would have to count the “yes” answers and then display them in a text field.

How can that be automated?

The first thing you should do is to use a naming convention for your radio button groups. In the following example, I use the following convention: All radio button group names start with “RBGroup”, that is then followed by a period and an index. This means I have “RGBroup.1”, “RBGroup.2”, and so on. This makes it easy to iterate over all groups without having to know their exact names, and how many of them there are.

The next thing you do is to make sure that all radio buttons options are the same for all the groups that you want to process. If you want to use “yes/no” options, make sure that all “yes” option are spelled the same way – “Yes”, “YES”, and “yes” are considered to be different.

Now create one or more text fields that you set to read-only (you don’t want the user to try to override your calculations) and then use the following script as the custom calculation script for each of these “count” fields:

var testFor = "Choice1";
var groups = this.getField("RBGroup");
var ar = groups.getArray();
var cnt = 0;
for (var i=0; i<ar.length; i++) {
if (ar[i].value == testFor) {
cnt++;
}
}
event.value = cnt;

The only thing left to do is to change the first line and specify what the script is testing for (e.g. “Yes”, “No”, “Choice1”, and so on.

If you don’t have a naming convention for your radio button groups, you can still use the same approach, but you would have to list your group names in an array and then process the array to use the getField() method on each array element. 

This entry was posted in Acrobat, JavaScript, PDF, Tutorial and tagged , , , , . Bookmark the permalink.

8 Responses to How to Count Radio Button Choices in Acrobat JavaScript

  1. chris says:

    Is there a simple way to take this script and add an equation to it?

    I would like to add all of the “choice” up and then multiply by 4 and print out that number.

    example:
    if 25 radial buttons are filled this equation displays 25. I would like it to display 100

  2. Jana says:

    If I need some buttons to be worth 2 instead of 1, how would I alter this? It’s working great for a value of 1!

  3. Karl Heinz Kremer says:

    Jana, you can do this by expanding the “if” statement:


    if (ar[i].value == "Choice1") {
    cnt++;
    }
    else if (ar[i].value == "Choice2") {
    cnt = cnt + 2;
    }

    As long as you can describe when exactly you want to increment the count by 2, you should be able to express that in JavaScript. This has actually not much to do with Acrobat, or Acrobat’s JavaScript implementation, it’s just about the JavaScript core language. If you are having problems with what JavaScript can do, you may want to look into a good JavaScript tutorial.

  4. Karl Heinz Kremer says:

    Chris, you can certainly do that. See my advice I just gave Jana: This is not Acrobat specific, and you can do that by using just standard JavaScript syntax:


    event.value = cnt * 4;

  5. Vicki Shattuck says:

    Can you do the same thing with a List Box? I have two choices in a list box: yes and no. I would like to be able to calculate how many Y’s are in the boxes and how many N’s are in the boxes. Possible?

    Vicki

  6. Karl Heinz Kremer says:

    Vicki, if you use the same organization for your list boxes (e.g. ListBox.1, ListBox.2, …), you can use the same script and just test for the value you are interested in (e.g. “Yes”).

  7. Shauna says:

    I have multiple groups of 3 radio buttons (Choice1, Choice2, and Choice3. I have done this, but can’t get the script to calculate. Any ideas?

    var testFor = “Choice1”;
    var groups = [this.getField(“Group02”).value, this.getField(“Group03”).value, this.getField(“Group04”).value, this.getField(“Group05”).value, this.getField(“Group07”).value, this.getField(“Group08”).value, this.getField(“Group09”).value, this.getField(“Group10”).value, this.getField(“Group11”).value, this.getField(“Group12”).value, this.getField(“Group13”).value, this.getField(“Group14”).value, this.getField(“Group15”).value, this.getField(“Group16”).value, this.getField(“Group17”).value];
    var cnt =0;
    for (var i=0; i<groups.length; i++) {
    if (groups[i].value == testFor) {
    cnt++;
    }
    }
    event.value = cnt;

  8. Karl Heinz Kremer says:

    Shauna, you should remove the “.value” portion of all elements in your “groups” array – you are referencing the “value” property inside your loop. I usually only add field names to an array, and then use these field names in a loop – a lot less to type and easier to understand:


    var testFor = "Choice1";
    var groups = [
    "Group02", "Group03", "Group04", "Group05", "Group07", "Group08", "Group09",
    "Group10", "Group11", "Group12", "Group13", "Group14", "Group15", "Group16", "Group17"
    ];
    var cnt = 0;
    for (var i = 0; i < groups.length; i++) { if (this.getField(groups[i]).value == testFor) { cnt++; } } event.value = cnt;

Leave a Reply

Your email address will not be published. Required fields are marked *