How to Write a Checkbox Script to Increment and Decrement a Total in PDF Forms
Question details
The user needs a JavaScript solution for a PDF form checkbox that adds a specific value to a total field when checked, and subtracts that same value when unchecked.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with dynamic calculations based on user selections.
- Observed behavior
- The current MouseUp script successfully adds the value to the total when the checkbox is selected, but fails to subtract the value when the checkbox is cleared.
Ensure you have the exact field names for your checkboxes and total text fields ready, and verify that the values you want to add or subtract are formatted as numbers.
Use JavaScript to Evaluate the Checkbox State
Instead of only adding a value on a mouse click, check if the checkbox state is 'Off' to determine whether the script should add or subtract the value.
By validating the current state of the checkbox ('Off' versus a selected value), you can accurately dictate whether the script should increment or decrement the target total field. It is crucial to ensure that the text field strings are properly parsed into numeric values using the Number() function to avoid string concatenation errors.
Open your PDF form in edit mode, right-click the target checkbox field, and select 'Properties'.
Go to the 'Actions' tab, select 'Mouse Up' as the trigger, choose 'Run a JavaScript' from the Action drop-down menu, and click 'Add'.
Enter the following script: var field = this.getField('TotalFieldName'); var amount = Number(this.getField('ValueFieldName').valueAsString); if (this.getField('CheckboxFieldName').valueAsString !== 'Off') { field.value = Number(field.value) + amount; } else { field.value = Number(field.value) - amount; }
Replace 'TotalFieldName', 'ValueFieldName', and 'CheckboxFieldName' with your actual PDF form field names. Click 'OK', close the properties window, and test your form.

Create and Edit Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to manage text fields, checkboxes, and interactive elements easily. You can view, fill, and modify complex PDF forms without the need for expensive software.
- 1. Download WPS Office: Download and install WPS Office on your computer or mobile device.
- 2. Open the PDF File: Launch WPS PDF and open your interactive PDF document.
- 3. Fill or Edit the Form: Navigate to the Form tools to fill out checkboxes and calculate fields, or use the Edit tools to modify the document layout.
- 4. Save Your Changes: Click Save to preserve your filled data or edits securely.

Frequently Asked Questions
Why is my PDF checkbox script returning 'NaN'?
The 'NaN' (Not a Number) error occurs when the script attempts a mathematical calculation using a text string or a blank field instead of a number. Ensure you use the Number() function around your field values, such as Number(this.getField('fieldName').valueAsString).
How do I find the exact name of a PDF form field?
In PDF form editing mode, right-click the field and select Properties. The exact field name is displayed at the top of the General tab. Remember that field names in JavaScript are strictly case-sensitive.
Does the MouseUp trigger work better than MouseDown for checkboxes?
Yes, using the MouseUp trigger is highly recommended for interactive form fields. It executes the script only after the user releases the click, ensuring the checkbox's state has successfully updated to 'checked' or 'unchecked' before the calculation runs.
Can I apply this increment/decrement script to multiple checkboxes?
Yes, you can apply this logic to multiple checkboxes. However, you will need to reference the specific checkbox field name within each individual script, or create a document-level JavaScript function that passes the checkbox name dynamically.




