How to Count Selected PDF Checkboxes by Column with JavaScript
Question details
The user needs to calculate the total number of checked boxes within a specific column of a PDF form using JavaScript.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form with multiple rows and columns of checkboxes where a total count per column must be displayed in a calculation field.
- Observed behavior
- A custom calculation script is required to iterate through the checkboxes in the target column and return the sum of the selected ones to a total field.
Ensure your PDF form has clearly named checkbox fields and that you are using a PDF editor that supports JavaScript form calculations and field properties.
Use a Custom Calculation Script with a Field Array
Create an array of specific checkbox names and loop through them in the total field to tally the selected boxes.
This method involves listing the exact names of the checkboxes in your column. The script checks each field's export value to see if it is selected (i.e., not equal to 'Off') and increments the total count.
Open your PDF form in Edit mode, right-click the text box designated for the column total, and select 'Properties'.
In the Properties dialog, click on the 'Calculate' tab to set up the field's calculation parameters.
Select the 'Custom calculation script' option and click 'Edit'. Paste the following JavaScript code: var fields = ["checkbox 1", "checkbox 2"]; var total = 0; for (var i = 0; i < fields.length; i++) { var f = this.getField(fields[i]); if (f && f.value != "Off") total++; } event.value = total;
Click 'OK' to save the script, then close the Properties window. Switch to Preview or Form Fill mode and click the checkboxes to ensure the total updates accurately.

Iterate Through Checkboxes Using a Naming Convention
Use a structured naming convention (like row_1, row_2) to loop through fields dynamically without hardcoding every name.
Edit and Calculate PDF Forms Easily with WPS Office
WPS PDF provides robust tools for creating interactive forms, setting up checkboxes, and applying custom calculation scripts seamlessly. Manage your document logic without needing expensive software.
- 1. Open PDF in WPS Office: Launch WPS Office, open your PDF document, and navigate to the 'Form' tab on the top ribbon.
- 2. Access Interactive Fields: Click on 'Text Field' or 'Check Box' to edit existing form elements or draw new ones on the document.
- 3. Configure Properties and Scripts: Double-click the desired total field to open its Properties panel, switch to the 'Calculate' section, and easily input your custom JavaScript code.

Frequently Asked Questions
Why is my PDF checkbox calculation returning 0?
This usually happens if the checkbox export value doesn't match the one specified in your script. Ensure your script checks for the exact export value string (e.g., 'Yes'), or verify it is checking that the value is not equal to 'Off'.
Can I group checkboxes in a row to act like radio buttons?
Yes. You can achieve this by giving all checkboxes in the same row the exact same Name under their General properties, but assigning different Export Values to each under the Options tab. Selecting one will automatically deselect the others.
Where do I input the JavaScript code in a PDF form?
Right-click the target field (such as the text box meant to display the total) and select Properties. Navigate to the Calculate tab, choose the 'Custom calculation script' option, and click the Edit button to paste your code.




