How to Run JavaScript When Multiple PDF Checkboxes Are Selected
Question details
The user wants to execute a specific JavaScript action, such as displaying an alert, when at least two checkboxes are selected in a PDF form.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms that require conditional logic based on the number of selected checkboxes.
- Observed behavior
- Triggering a custom JavaScript event automatically when the selection count of specified checkboxes reaches two or more.
Ensure your PDF editor supports JavaScript execution in form fields. You will also need to know the exact field names of the checkboxes you intend to monitor.
Use a Hidden Text Field with a Custom Calculation Script
By placing a custom calculation script inside a hidden text field, the PDF reader will automatically evaluate the script whenever any checkbox state changes.
PDF forms use dynamic event triggers to run scripts. Instead of copying the same code into the 'Mouse Up' action of every single checkbox, a hidden text field provides a centralized way to monitor form changes. The calculation event of this hidden field is triggered whenever any data in the form is modified.
Open your PDF editor in Form Preparation mode. Insert a new Text Field into the document, open its Properties, and under the General tab, set its Form Field properties to 'Hidden' so it does not display to the user.
In the Text Field Properties window, navigate to the 'Calculate' tab. Select the option for 'Custom calculation script' and click the 'Edit' button to open the JavaScript editor.
In the editor, start by defining an array containing the exact names of your checkboxes. For example: `var fields = ["Checkbox1", "Checkbox2", "Checkbox3", "Checkbox4"];`
Add a loop to check each field's status and count the ones that are checked (not 'Off'). Then, add an if-statement to run your action. Use this code snippet: `if (event.source != null && fields.indexOf(event.source.name) != -1) { var counter = 0; for (var i in fields) { var f = this.getField(fields[i]); if (f.valueAsString != "Off") counter++; } if (counter >= 2) { app.alert("More than two checkboxes are selected.", 3); } }`

Create and Edit Interactive PDF Forms with WPS PDF
WPS Office includes a comprehensive PDF editor that allows you to easily create, modify, and manage interactive PDF forms. Whether you need to insert checkboxes, text fields, or drop-down menus, WPS PDF provides an intuitive interface to build professional forms.
- 1. Launch WPS Office: Download and open WPS Office, then select the 'PDF' module from the home screen.
- 2. Open Your Document: Open an existing PDF document or create a new blank PDF to start building your form.
- 3. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive form controls.
- 4. Insert Checkboxes: Click on the 'Check Box' icon and draw the fields directly onto your document, then double-click to adjust their names and properties.
- 5. Save and Distribute: Save your completed interactive PDF form. It is now ready to be filled out by any standard PDF reader.

Frequently Asked Questions
How do I check the value of a PDF checkbox using JavaScript?
In PDF JavaScript, an unchecked checkbox typically returns the string 'Off'. When checked, it returns its designated export value (often 'Yes' or 'On'). You can check this by using `this.getField("CheckboxName").valueAsString`.
Why use a hidden text field instead of attaching the script directly to the checkboxes?
Attaching the script to a hidden text field's calculation event ensures the script evaluates automatically whenever any field value changes. This avoids the need to copy and maintain the same script on every individual checkbox's 'Mouse Up' event.
Can I hide or show other form fields based on this checkbox count?
Yes. Instead of triggering an alert message, you can modify the script to change the display properties of other fields. For example, use `this.getField("TargetField").display = display.visible;` when the counter reaches two.
Why is my PDF JavaScript not triggering when I click a checkbox?
Ensure that JavaScript is enabled in your PDF reader's security preferences. Additionally, verify that the field names in your JavaScript array match the actual checkbox names exactly, as JavaScript is case-sensitive.




