How to Calculate PDF Checkbox or Radio Button Values with JavaScript
Question details
The user needs to create conditional form calculations in a PDF that assign numeric values to Yes and No choices based on radio button or checkbox selections.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms that require automated conditional calculations based on user input from radio buttons or checkboxes.
- Observed behavior
- Needs to calculate and display a numeric result dynamically based on the selections made across multiple radio button groups or checkboxes.
Ensure you have a PDF editor that supports form field editing and JavaScript, such as Adobe Acrobat Pro or WPS Office, and verify that your radio buttons and checkboxes have assigned export values.
Write a Custom JavaScript Calculation Script
Use JavaScript to read the selected export values of radio buttons or checkboxes and assign conditional numeric results to a calculation text field.
PDF forms use JavaScript to handle complex conditional logic that standard calculation functions cannot process. By reading the field value as a string, you can test whether the user selected 'Yes' or 'No' and assign the corresponding numerical value to the output field.
Open your PDF document in your PDF editor (e.g., Adobe Acrobat) and navigate to the 'Prepare Form' or 'Form' editing mode.
Double-click your radio buttons or checkboxes to open their properties. In the 'Options' tab, set the 'Radio Button Choice' or 'Export Value' to text strings like 'Yes' or 'No'.
Insert a new Text Field where you want the final calculated result to appear. Double-click this new text field to open its Properties window.
Navigate to the 'Calculate' tab in the Properties window, select 'Custom calculation script', and click the 'Edit' button. Enter the following script logic: var q1 = this.getField("Question 1").valueAsString; if (q1 == "Yes") event.value = 10; else if (q1 == "No") event.value = 20; else event.value = "";
Close the form editing mode and interact with the radio buttons or checkboxes to verify that the calculation text field updates automatically based on your selections.

Edit and Manage Interactive PDF Forms with WPS Office
WPS Office provides robust PDF editing capabilities, allowing you to easily fill, create, and manage interactive PDF forms. Whether you need to handle checkboxes, radio buttons, or text fields, WPS PDF makes it seamless and highly compatible.
- 1. Open PDF: Open your PDF document in WPS Office and navigate to the 'Form' tab on the top ribbon.
- 2. Insert Form Controls: Use the interactive form tools to easily drag and drop checkboxes, radio buttons, or text fields onto your page.
- 3. Configure Properties: Double-click the inserted fields to configure their properties, appearance, and export values to match your specific needs.
- 4. Save and Export: Save the document to preserve your interactive PDF form features so others can easily fill them out.

Frequently Asked Questions
Why is my JavaScript calculation not updating automatically in the PDF?
If your calculation isn't updating dynamically, ensure that the fields involved in the calculation have the 'Commit selected value immediately' option checked in their properties (under the Options tab). Also, verify the field calculation order if you have multiple dependent calculations.
How do I calculate values from multiple radio button groups together?
You can define multiple variables in your custom script, such as var q1 = this.getField("Group 1").valueAsString; and var q2 = this.getField("Group 2").valueAsString;. Then, use conditional statements to determine the numeric value of each selection and add those numbers together before assigning the final sum to event.value.
Can I assign numerical export values directly to radio buttons instead of using JavaScript?
Yes. Instead of using text strings like 'Yes' or 'No', you can set the Export Value of the radio button directly to a number (e.g., '10' or '20'). Once configured, you can use the built-in calculation options (like 'Value is the sum of the following fields') in the result field's Calculate tab without needing to write any custom JavaScript.




