How to Use JavaScript to Show PDF Layers Based on Checkboxes
Question details
The user needs to configure a PDF form to show or hide specific layers when multiple checkboxes are selected.

- Product
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where checking specific boxes reveals or hides certain content layers.
- Observed behavior
- The user needs to determine how to evaluate the states of multiple checkboxes simultaneously in JavaScript to control the visibility of corresponding layers.
Ensure your PDF editor supports JavaScript for interactive forms and that your document layers (Optional Content Groups) are properly created and named before adding scripts.
Evaluate Checkboxes Independently (Recommended)
Assign an independent JavaScript condition to each checkbox to toggle its associated layer visibility without interfering with other checkboxes.
When dealing with multiple checkboxes, it is generally best to evaluate each checkbox independently rather than creating complex nested 'if-else' statements for every combination.
First, determine the exact relationship: one-to-one, one-to-many, or many-to-many. By treating each checkbox as an independent trigger, the PDF can dynamically update multiple layers at once when multiple boxes are checked.
Open your PDF editor and navigate to the Form Editing or Prepare Form mode.
Right-click the first checkbox, select 'Properties', and go to the 'Actions' tab.
Choose 'Run a JavaScript' as the action type and click 'Add'.
Write a script that retrieves the layer (OCG) by name and sets its state property based on the checkbox value (e.g., setting the state to true if the box is checked, and false if it is unchecked).
Repeat this process for Checkbox 2 and Checkbox 3, linking each independent script to its respective layer.

Use a Centralized Document-Level Script
Create a document-level script if your layers should only appear when specific combinations of checkboxes are selected together.
Manage Interactive PDF Forms Easily with WPS Office
WPS Office provides excellent PDF editing tools that let you handle forms, edit text, and customize document layouts. It is the perfect all-in-one productivity suite for your daily document tasks.
- 1. Open PDF in WPS Office: Launch WPS Office and open your existing PDF document.
- 2. Navigate to Form Tools: Click on the 'Form' tab located in the top ribbon menu to access interactive tools.
- 3. Add Checkboxes: Select the 'Check Box' tool and click anywhere on your document to place the field.
- 4. Customize Properties: Right-click the checkbox to modify its appearance, name, and export values.

Frequently Asked Questions
Why aren't my PDF layers hiding when I uncheck the box?
This usually happens if the JavaScript only sets the layer state to true when the box is checked, but lacks the corresponding 'else' condition to set it to false when unchecked. Ensure your script evaluates both the checked and unchecked states.
Can I link one checkbox to multiple layers?
Yes, you can retrieve multiple layers (OCGs) in your script and set the state property for all of them to true or false within the same block of code triggered by a single checkbox.
Are PDF layers and interactive scripts supported in all PDF viewers?
While most advanced desktop viewers support layers and JavaScript forms, some basic mobile PDF readers or web browsers might not execute the JavaScript correctly, causing the forms to behave unpredictably.
How do I find the exact name of my PDF layer for the script?
Open the Layers panel in your PDF editor. The exact name listed there is what you must use as the string parameter in your JavaScript function when querying the document's layer array.




