PDF Editing Issues

How to Conditionally Show PDF Form Fields and Layers Using JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Nov 3, 2026 869 views

Question details

The user needs a method to dynamically reveal a hidden form field and an optional content layer in a PDF document only when a specific numerical condition is met in another input field.

How to Conditionally Show a PDF Form Field and Content Layer with JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Creating an interactive PDF form where dependent fields and elements (like the 'Width' field and 'Layer 2') stay hidden until a user enters a value greater than 5 into the trigger field ('Length').
Observed behavior
The dependent field and layer must actively switch their visibility state from hidden to visible based on the live input data of the trigger field.
Before you start

Ensure that your PDF editing software supports JavaScript for interactive form validations and verify the exact names of your target form fields and Optional Content Groups (OCGs) before applying the script.

Solution 1Recommended

Apply a Custom Validation Script to the Trigger Field

Use JavaScript within the trigger field's properties to evaluate the inputted number and programmatically toggle the visibility of both the dependent field and the specific content layer.

By binding a custom JavaScript validation to the 'Length' field, the PDF viewer will evaluate the code every time the field's value changes. The script calculates a boolean condition and uses it to update the display property of the 'Width' field and the state property of 'Layer 2'.

1
Open Trigger Field Properties

In your PDF form editor, right-click the 'Length' field (the field that users will interact with) and select 'Properties' from the context menu.

2
Navigate to the Validate Tab

Switch to the 'Validate' tab in the Properties dialog box and select the option to 'Run custom validation script'.

3
Enter the JavaScript Code

Click the 'Edit' button and paste the following code: `var show = Number(event.value) > 5; this.getField('Width').display = show ? display.visible : display.hidden; var ocgs = this.getOCGs(); if (ocgs) { for (var i in ocgs) { if (ocgs[i].name == 'Layer 2') { ocgs[i].state = show; break; } } }`.

4
Save and Test

Click 'OK' to save the script, close the properties window, and exit form editing mode to test the logic by entering numbers higher and lower than 5.

Apply a Custom Validation Script to the Trigger Field
Form Reset Logic Required: Keep in mind that this validation code will not automatically re-hide the field and layer when the form is reset. You may need to attach an additional script to your reset button to restore the initial hidden states.
Advanced PDF Form Editing

Easily Create Interactive PDF Forms with WPS Office

WPS Office provides robust PDF editing tools, allowing you to create advanced interactive forms, configure field properties, and manage document layers efficiently without needing expensive specialized software.

  1. 1. Open Your PDF: Launch WPS Office and open the PDF document containing your form fields.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive field creation and editing features.
  3. 3. Edit Field Properties: Double-click your desired text field to open its properties, where you can easily configure appearance, actions, and validation settings.
  4. 4. Save Document: Once your configuration is complete, save the document to preserve your interactive form logic.
Fully compatible with standard PDF interactive forms and JavaScript validations.Intuitive interface for adding, naming, and managing text fields, checkboxes, and buttons.Seamless handling of document layers and advanced field properties.Lightweight, fast-loading, and highly cost-effective compared to traditional premium PDF editors.
microsoft office alternative - wps office

Frequently Asked Questions

What is an OCG in a PDF document?

OCG stands for Optional Content Group, which is the technical term for a 'layer' in a PDF. Elements assigned to an OCG can be toggled visible or hidden programmatically via JavaScript or manually via the Layers panel in a PDF viewer.

Why doesn't the layer hide when the user clears the value in the Length field?

If the field validation requires a specific event like pressing 'Enter' or losing focus, simply deleting the value might not immediately trigger the script. You may need to ensure 'Commit selected value immediately' is checked in the field options, or attach the script to a Keystroke event instead of Validation.

How do I ensure the field hides again when the form is cleared or reset?

Because the validation script only runs when the trigger field's value changes, a form reset might bypass it. You should attach a custom Document Action script or add an explicit script to your 'Reset Form' button that manually sets `this.getField('Width').display = display.hidden;` and reverts the OCG state.

Can I conditionally show multiple fields using the same script?

Yes. You can duplicate the `this.getField('FieldName').display` line for as many form fields as you need to control. Simply replace 'FieldName' with the exact names of your additional target fields.