How to Average PDF Radio Button Values Using JavaScript
Question details
The user needs to calculate the average of selected radio buttons in a PDF form using JavaScript, with specific conditions prioritizing values 1 or 2 and applying custom rounding rules.

- Product
- PDF Form
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires complex calculations based on user selections in radio button groups.
- Observed behavior
- The form needs to output 1 or 2 if either is selected, ignore unselected 'Off' values, and apply non-standard thresholds (such as 4.6 to 5.0 returning 5) for the average of the remaining selections.
Ensure that your PDF editor supports JavaScript editing for forms and that all your radio button groups have exactly defined field names and export values (1 through 5).
Implement Custom JavaScript for Conditional Averaging
Use a custom calculation script on the result field to read radio button values, prioritize specific answers, and calculate custom averages while ignoring default 'Off' states.
Standard calculation fields in PDF forms cannot handle complex logic like custom rounding or conditional priorities. By writing a custom JavaScript calculation, you can define exactly how missing values are handled and enforce strict priority rules before the average is calculated.
Open your PDF form in edit mode, right-click a radio button, and select Properties. In the Options tab, ensure every button in a group has a unique Export Value (e.g., 1, 2, 3, 4, 5) and that grouped buttons share the exact same Field Name.
Right-click the text field or button where the result should appear and select Properties. Navigate to the Calculate tab, select 'Custom calculation script', and click the Edit button.
Write a loop to retrieve the values of each radio button group using `this.getField("FieldName").value`. Add logic to check if the value is 'Off' and exclude it from the count. Add an `if` condition that immediately sets the event value to 1 or 2 if either of those specific export values is detected.
If no 1 or 2 is selected, sum the remaining valid values and divide by the total valid count to get the average. Use `if/else if` statements to define your custom thresholds (e.g., `if (average >= 4.6) { event.value = 5; }`) instead of standard math rounding functions.
Create and Edit Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive PDF editor that allows you to seamlessly design forms, insert radio buttons, text fields, and manage form properties for interactive documents.
- 1. Open your PDF form: Launch WPS Office and open the PDF document you want to turn into an interactive form.
- 2. Access the Form tools: Navigate to the 'Form' tab located in the top ribbon menu to reveal the form control options.
- 3. Add form fields: Click on 'Radio Button' or 'Text Field' and draw the element on your document page.
- 4. Configure field properties: Double-click the newly created field or right-click and select 'Properties' to set field names and export values.

Frequently Asked Questions
Why is my PDF radio button returning an 'Off' value in JavaScript?
In PDF forms, unselected radio buttons default to a string value of 'Off'. Your custom JavaScript must explicitly check for this string and ignore it to avoid calculation errors.
Can I use standard math rounding in PDF JavaScript?
Yes, standard functions like `Math.round()` work in PDF JavaScript. However, if you need custom thresholds (like rounding up at 4.6 instead of 4.5), you must write custom `if/else` conditions to handle the ranges manually.
How do I find the export value of a radio button?
Enter form editing mode, right-click the radio button, select Properties, and go to the Options tab. The 'Radio Button Choice' or 'Export Value' field displays the exact value passed to your script.
Do these JavaScript calculations work in all PDF viewers?
Complex custom JavaScript calculations may not function correctly in basic web browsers or simple mobile PDF viewers. It is recommended that users fill out the form using robust desktop software like WPS Office or Adobe Acrobat.




