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.

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

  9. kumar says:

    converting HTML to PDF which as Radiobuttons. After it renders the PDF unable to get radiobutton value. Below is the code

    for some reason it is displaying Group as “radio”, however i’m not able to get radiobutton value

    var f = this.getField(“radio”);
    tried all below,
    app.alert(this.getField(f.name+”.1″).value);
    app.alert(this.getField(f.name+”.0″).value);

    for every alert it is returning the radio button that is selected.

    not sure why it is not displaying “testradioName” as Group

  10. Karl Heinz Kremer says:

    Kumar, I am sorry, but I don’t have any experience with HTML forms converted to PDF.

  11. Vicki says:

    I tried adapting Karl’s JavaScript suggestions highlighted to Shauna earlier for my specific needs, but having issues…

    I want to be able to count the number of ‘A’, ‘B’ and ‘C’ answers for each question. I have saved each radio button as A, B or C in groups using the following format ‘Question1’, ‘Question2’ etc. etc.

    The script I adapted doesn’t seem to work though – would gratefully appreciate any suggestions on a fix!

    Thanks in advance!

    My Script:
    var testFor = “A”;
    var groups = [
    “Question1”, ”Question2”, “Question3”, “Question4”, “Question5”, “Question7”, “Question8”, “Question9″, “Question10”, “Question11”, “Question12”, “Question13”, “Question14”, “Question15”, “Question16”, “Question17”, “Question18”, “Question19”, “Question20”, “Question21”, “Question22”, “Question23”, “Question24”
    ];
    var cnt = 0;
    for (var i = 0; i < groups.length; i++) { if (this.getField(groups[i]).value == testFor) { cnt++; } } event.value = cnt;

  12. Karl Heinz Kremer says:

    Vicky, based on what you posted, that should work. What is the behavior? Are you getting any errors on the Javascript console?

  13. Jay says:

    Hi. I have tried the above and am having no luck. I have 8 groups named “Group1, Group2, Group3, etc.. Each group has 2 radio button responses named: “Yes” “No” and two of those groups has a 3rd radio button response: “NA”.

    I am needing to count only the “No” responses for all these groups. Can anyone help me with a javascript for this. Thanks in advance.

  14. Karl Heinz Kremer says:

    Jay, with your naming convention, the above code will not work: The trick is to have a period between the base name and the index (e.g. “Group.1”). However, a small change to the script will also work with the fields you have:

    var cnt = 0;
    for (var i=1; i<=8; i++) {
        if (this.getField("Group" + i).value == "No") {
            cnt++;
        }
    }
    event.value = cnt;
    
  15. Jay says:

    Thank you. That worked!!!

  16. Mary says:

    Karl,
    This snippet of code made my day. Thank you for providing a simple (and free) explanation and solution.
    Cheers!

  17. Chris McNair says:

    This one worked for me. Thank you so much!
    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;

  18. Susan Godbey says:

    This worked so well! Thank you for posting this!

  19. Jessie says:

    This worked PERFECTLY. Thank you!!!

Leave a Reply

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