How to Count PDF Radio-Button Selections Using Acrobat JavaScript
Question details
The user needs to calculate the total number of specific selections across multiple radio-button groups in a PDF form.

- Product
- Adobe Acrobat PDF Forms
- Device & OS
- not provided
- Scenario
- Developing an interactive PDF form where specific user choices (like 'No' or 'Yes') across multiple radio-button groups need to be automatically tallied into a total field.
- Observed behavior
- Instead of manually adding every single field, the developer requires an efficient JavaScript loop to read export values, convert them to numbers if necessary, and increment a sum.
Before writing your script, ensure all your radio-button groups share a consistent, sequential naming convention (e.g., 'Group0', 'Group1') and that clear export values are assigned to each button choice.
Use a JavaScript Loop to Calculate the Sum
A highly reliable script utilizing a JavaScript loop to dynamically check each radio-button group and sum up specific string or numeric values without manually calling every field.
If you have many radio-button groups that follow a common naming pattern, using a 'for' loop simplifies the calculation code drastically. This method checks the export value of each group and increments your sum only when the selected value matches your target choice.
Ensure your radio buttons are named with a sequential pattern. For instance, name them Group0, Group1, Group2, up to Group9.
In Adobe Acrobat, activate the Prepare Form tool. Double-click the text field where you want the final count to appear to open its Properties window.
Click on the 'Calculate' tab inside the Properties window, and select the 'Custom calculation script' option.
Click the 'Edit' button and input your loop script. For example: var nSum = 0; for (var i = 0; i < 10; i++) { var cVal = this.getField('Group' + i).value; nSum += (cVal == 'No') ? 1 : 0; } event.value = nSum;
Close the JavaScript editor and the Properties window. Exit the form editing mode and select different radio buttons to verify that the total updates automatically.

Use Built-in Calculation for Numeric Export Values
If you are designing a new form and only need basic counting, you can assign numerical export values to the radio buttons and use the built-in sum function.
Create and Manage Interactive PDF Forms with WPS Office
While custom JavaScript is useful for advanced tasks in Acrobat, WPS Office provides a streamlined PDF editor that lets you easily create, format, and fill interactive PDF forms. Whether you need to insert radio buttons or manage document formatting, WPS Office simplifies the process.
- 1. Open your PDF file: Launch WPS Office and open your existing PDF document using the WPS PDF tool.
- 2. Access the Form tab: Navigate to the 'Form' tab on the top ribbon to access the interactive form creation tools.
- 3. Insert form elements: Click the 'Radio Button' or 'Text Field' icon, then click and drag on the document page to position your elements.
- 4. Configure properties: Double-click the newly created form fields to adjust their group names, export values, and appearance.
- 5. Save and distribute: Save your interactive PDF to share it seamlessly across different devices and platforms.

Frequently Asked Questions
Why is my PDF JavaScript returning 'Off' instead of a numeric value?
When a radio-button group has no selection made by the user, its default value is returned as 'Off'. You should add a condition in your JavaScript to check if the field value is 'Off' and treat it as 0 to prevent calculation errors.
Do I have to use a loop to count radio buttons?
No. You can manually type out the addition of each field using 'this.getField('GroupName').value'. However, a loop drastically reduces the amount of code required and is much easier to maintain, especially for forms with 10 or more groups.
Can I use this JavaScript loop to count checkboxes instead?
Absolutely. The logic remains the same. Ensure your checkboxes are sequentially named, and check if their '.value' matches the checked export state (e.g., 'Yes') within the loop to increment your total.




