How to Show or Hide a PDF Button Based on a Field Value
Question details
The user needs to control the visibility of a PDF form button based on whether the numeric value entered into a separate input field falls within a specific range.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with dynamic elements that respond to user input.
- Observed behavior
- The user attempted to place the visibility script within the button object itself, which failed because the button cannot detect value changes in another field without a proper event trigger.
Ensure you are using a PDF editor that supports advanced form editing and custom JavaScript, and keep a backup copy of your original form before modifying any scripts.
Use Custom JavaScript in the Source Field's Validation Event
Apply the visibility script to the calculation or validation event of the field being modified, rather than the button. This ensures the script triggers immediately when the user changes the field's value.
A common mistake when trying to hide or show an object in a PDF form is placing the script in the target object itself. Scripts must be attached to the event that triggers the change.
By adding the script to the validation or calculation event of the input field, the PDF reader will evaluate the condition (such as checking if a number is between 18 and 22) every time the field's value is updated.
Open your interactive PDF document in a capable PDF editor and navigate to the form creation or editing tools.
Right-click the input field that will determine the button's visibility (e.g., the 'ZFW' field) and select 'Properties'.
Navigate to the 'Validate' or 'Calculate' tab, choose the option to run a custom JavaScript, and open the script editor.
Enter the following script, ensuring you use raw numbers without quotation marks for numeric comparison: if (event.value >= 18 && event.value <= 22) { this.getField("Button1").display = display.visible; } else { this.getField("Button1").display = display.hidden; }
Save the properties, exit form editing mode, and type different numbers into the field to verify the button appears and disappears correctly.

Easily Fill and Manage Interactive PDF Forms with WPS PDF
WPS Office provides a comprehensive PDF editor that seamlessly handles interactive PDF forms. Whether you need to fill out dynamic fields, trigger embedded scripts, or manage essential document data, WPS PDF delivers a smooth and reliable experience.
- 1. Launch WPS PDF: Open WPS Office and select the 'PDF' tab to access the dedicated PDF toolset.
- 2. Open Your Interactive Form: Import or open your existing interactive PDF document containing dynamic form fields.
- 3. Interact with Form Fields: Click into the input fields to enter data; dynamic buttons and elements will respond automatically to your inputs.
- 4. Save and Export: Once the form is filled out, click 'Save' or 'Export' to secure your completed document.

Frequently Asked Questions
Why doesn't the script work when placed in the button's action event?
A button's action event is only triggered when the button itself is clicked or hovered over. To change a button's visibility based on what happens in another field, the script must be placed in the source field's validation or calculation event, which detects the value change.
Should I use quotation marks around the numbers in my JavaScript condition?
No. If you use quotation marks (e.g., "18"), the script treats the value as text instead of a numeric value. For logical range comparisons like greater than or less than, you must use raw numbers.
How can I apply this visibility script to multiple buttons at once?
You can add multiple statements inside the same if/else block in your script. Simply add another line like this.getField("Button2").display = display.visible; for every additional button you want to control based on that single field's value.




