How to Use JavaScript for PDF Form Calculations Based on Conditions
Question details
The user needs to set up a custom JavaScript calculation in a PDF form where a field's value is dynamically mapped from a radio button selection, but only if a specific grade field meets a numerical condition.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires conditional logic to calculate output values accurately based on multiple user inputs.
- Observed behavior
- The target field calculation needs to trigger and map values from radio buttons exclusively when the grade field value is equal to or greater than 1; otherwise, the field must remain blank.
Ensure your PDF editor supports JavaScript for form fields, and note down the exact field names for both your grade text box and radio button group before writing the script.
Apply a Custom Calculation Script to the Target Field
Add a conditional JavaScript code snippet to your target field's calculation properties to evaluate the grade field and output the mapped radio button value accordingly.
This script defines an array of values corresponding to the radio buttons. It checks if the grade is valid (not a dash and greater than or equal to 1). If the condition is met, it pulls the associated value from the radio button group; if not, it leaves the field blank.
Open your PDF form in edit mode, right-click the target text field where the calculated result should appear, and select 'Properties'.
Navigate to the 'Calculate' tab inside the Properties window and select the 'Custom calculation script' option.
Click the 'Edit' button and paste the following conditional script: var values = {"Radio1":"4","Radio2":"3","Radio3":"5"}; var grade = this.getField("Item1").valueAsString; if (grade != "-" && Number(grade) >= 1) event.value = values[this.getField("RadioSet").valueAsString] || ""; else event.value = "";
Adjust 'Item1' and 'RadioSet' in the script to match the exact field names used in your specific PDF document, then click 'OK' and close the properties panel to test your form.

Manage and Fill Interactive PDF Forms with WPS Office
WPS PDF provides a reliable and lightweight set of tools to read, edit, and fill interactive PDF forms quickly. It fully supports standard PDF form fields, making it easy to manage your documents without complex setups.
- 1. Open WPS PDF: Launch WPS Office on your device and click on 'PDF' from the main menu to open the PDF editor.
- 2. Load Your PDF Form: Open the interactive PDF form document that requires filling or reviewing.
- 3. Fill in the Fields: Click directly into the text boxes to input text, or click on radio buttons and checkboxes to make your selections.
- 4. Save and Export: Once the form is filled out correctly, click 'Save' or export the document to retain your inputted data.

Frequently Asked Questions
Why isn't my JavaScript calculating the PDF field automatically?
Form fields might not update automatically if the calculation order is incorrect. Check the 'Field Calculation Order' settings in your PDF editor to ensure the dependent field is calculated last, after the grade and radio button inputs.
Can I use JavaScript in all PDF viewers?
Not all PDF viewers support custom JavaScript calculations. While standard editors like Adobe Acrobat and advanced PDF tools process these scripts perfectly, basic web browser PDF viewers or default mobile viewers might not execute the code.
How do I find the exact field name in a PDF form?
Enter form editing mode in your PDF software. Double-click or right-click the specific form field and select 'Properties'. The exact field name will be listed under the 'General' tab.




