PDF Editing Issues

How to Write a Checkbox Script to Increment and Decrement a Total in PDF Forms

Anum MushtaqAnum Mushtaq Oct 28, 2026 869 views

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.

How to Create a Checkbox Script to Increment and Decrement Totals in PDF Forms
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Field Properties

Open your PDF form in edit mode, right-click the target checkbox field, and select 'Properties'.

2
Navigate to the Actions Tab

Go to the 'Actions' tab, select 'Mouse Up' as the trigger, choose 'Run a JavaScript' from the Action drop-down menu, and click 'Add'.

3
Enter the JavaScript Code

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; }

4
Update Field Names and Test

Replace 'TotalFieldName', 'ValueFieldName', and 'CheckboxFieldName' with your actual PDF form field names. Click 'OK', close the properties window, and test your form.

Use JavaScript to Evaluate the Checkbox State
Data Types Matter: Always ensure your amount variables are strictly numeric using Number(). Otherwise, a value of 5 and 10 might be combined as '510' instead of calculating to 15.
Edit Interactive PDFs Easily

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. 1. Download WPS Office: Download and install WPS Office on your computer or mobile device.
  2. 2. Open the PDF File: Launch WPS PDF and open your interactive PDF document.
  3. 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. 4. Save Your Changes: Click Save to preserve your filled data or edits securely.
Easily interact with checkboxes, text fields, and dynamic PDF form elementsFully compatible with standard PDF forms and Adobe Acrobat filesLightweight, fast, and user-friendly interface for all your document needsEdit PDF text, images, and pages with just a few clicks
QA img-9

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.