How to Cycle PDF Buttons to Show and Hide Fields using JavaScript
Question details
The user wants to create a repeating sequence where PDF form buttons or fields become visible and hidden after successive clicks using a reusable script.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with dynamic button visibility states that loop in a cycle.
- Observed behavior
- Needs an efficient JavaScript method to toggle visibility states of multiple form field groups without manually rewriting scripts for every individual button.
Ensure you are using a PDF editor that supports JavaScript execution for form fields, and save a backup copy of your PDF form before adding or editing custom scripts.
Use Dynamic Field Naming and Visibility States in JavaScript
Control button visibility cycles by checking current states and using a shared prefix for field groups, allowing a single script to be reused.
By naming fields with a shared hierarchical prefix (such as Group1.Button1 and Group1.Button2), you can use the event.targetName property to identify the group dynamically. This prevents the need to hardcode field names and rewrite scripts for every individual button group in your document.
Open your PDF form in edit mode and access the properties of your target form fields.
Rename your buttons or fields using a consistent hierarchical naming convention, such as 'Group1.Btn1' and 'Group1.Btn2'.
Navigate to the Actions tab of your trigger button, select 'Mouse Up' as the trigger, and choose 'Run a JavaScript' as the action.
Write a script that extracts the group name using event.targetName and uses an if/else conditional statement to check the display property of the related buttons.
Program the logic so that if both buttons are hidden, Btn1 is set to visible. If Btn1 is visible, the script hides it and shows Btn2, thereby creating a continuous cycle upon each click.

Implement a Simple Two-Button Toggle Check
A straightforward approach to cycle through states by explicitly checking if buttons are hidden on every individual click.
Create and Manage Interactive PDF Forms in WPS Office
WPS Office provides robust PDF editing tools, allowing you to design interactive forms, manage form fields, and easily adjust document properties for a professional workflow.
- 1. Open PDF in WPS: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view interactive form tools.
- 3. Add Form Elements: Insert Push Buttons, Check Boxes, Dropdowns, or Text Fields into your document as needed.
- 4. Edit Properties: Right-click the inserted field to access 'Properties' and customize its appearance, values, and actions.

Frequently Asked Questions
Why is my PDF JavaScript not executing when I click the button?
JavaScript may be disabled in your PDF viewer's security settings. Check the preferences or security settings of your PDF reader to ensure JavaScript execution is allowed for interactive forms.
What is event.targetName in Acrobat JavaScript?
event.targetName is a property that returns the exact name of the field that triggered the current event. It is highly useful for dynamically identifying which button was clicked without hardcoding the field name into the script.
Can I hide a PDF button permanently using JavaScript?
Yes, you can permanently hide a button by setting its display property to display.hidden within your JavaScript logic. It will remain hidden until another script explicitly changes it back to display.visible.




