How to Use JavaScript to Compare Values and Hide Fields in PDF Forms
Question details
The user needs to use JavaScript in a PDF form to compare calculated totals, return zero if one total exceeds another, and dynamically hide or show the contributing fields without deleting the entered values.

- Product
- PDF Forms / Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms with conditional logic for calculated fields and field visibility.
- Observed behavior
- The goal is to dynamically return a value of 0 and hide specific contributing fields whenever a calculated total exceeds a specific comparison value, and restore visibility otherwise.
Ensure you have access to a PDF editor that supports advanced JavaScript custom calculation scripts, such as Adobe Acrobat Pro, and verify the exact names of your form fields before applying the code.
Apply a Custom Calculation Script to the Target Field
Use a custom JavaScript in the calculated field to perform the comparison and conditionally control the visibility of contributing fields.
Placing the code directly in the target calculated field's custom calculation script ensures that manual entries are not overwritten and that the field updates dynamically whenever the dependent values change.
The script retrieves the values, compares the sums, and uses the 'display.hidden' and 'display.visible' properties to manage field visibility without destroying the underlying data.
Open the PDF form in Edit mode, right-click the target calculated field where the result will be displayed, and select 'Properties'.
Navigate to the 'Calculate' tab, select the 'Custom calculation script' option, and click the 'Edit' button to open the JavaScript editor.
Retrieve the contributing fields and the comparison field by declaring variables. For example: var a = this.getField("fielda"); var b = this.getField("fieldb"); var y = this.getField("compareField").value;
Write an if/else statement to compare the sums. If the calculated sum (x) is less than the comparison value (y), set the event value to 0 and hide the fields (e.g., a.display = display.hidden). Otherwise, set the event value to the sum and restore visibility (e.g., a.display = display.visible).
Click 'OK' to save the script, close the properties dialog, and test the form by entering values to verify the dynamic visibility and calculation.

Easily Manage PDF Forms and Documents with WPS Office
While advanced JavaScript calculations require specialized scripting tools, WPS Office provides a comprehensive, user-friendly PDF editor to view, fill, edit, and manage standard PDF forms effortlessly.
- 1. Open Your PDF: Launch WPS Office and open your PDF document containing the forms.
- 2. Access the PDF Tools: Navigate to the 'PDF' or 'Edit' tab on the top ribbon to access the form-filling and editing tools.
- 3. Fill and Modify: Click on the interactive form fields to input your data or use the editing tools to add text, images, and annotations.
- 4. Save and Export: Once your form is filled out, click 'Save' or export the document to securely share your finalized PDF.

Frequently Asked Questions
Why aren't my PDF form fields calculating automatically?
Ensure that the form calculation order is set correctly in your PDF editor. If a dependent field calculates before the contributing fields are updated, the final result may be incorrect or delayed.
Can I hide a PDF form field without deleting its value?
Yes. By changing the field's display property to 'hidden' (e.g., using `display.hidden` in JavaScript), the field becomes invisible to the user but securely retains its entered value for backend calculations.
Where do I add custom calculation scripts in a PDF form?
Right-click the specific form field you want to calculate, go to 'Properties', select the 'Calculate' tab, choose 'Custom calculation script', and click 'Edit' to enter your JavaScript code.




