PDF Editing Issues

How to Cycle PDF Buttons to Show and Hide Fields using JavaScript

Steve KSteve K Nov 3, 2026 869 views

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.

How to Cycle PDF Buttons to Show and Hide Fields using JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Form Properties

Open your PDF form in edit mode and access the properties of your target form fields.

2
Rename Fields

Rename your buttons or fields using a consistent hierarchical naming convention, such as 'Group1.Btn1' and 'Group1.Btn2'.

3
Add JavaScript Action

Navigate to the Actions tab of your trigger button, select 'Mouse Up' as the trigger, and choose 'Run a JavaScript' as the action.

4
Write Dynamic Script

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.

5
Set Display Logic

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.

Use Dynamic Field Naming and Visibility States in JavaScript
Testing PDF Scripts: Testing form scripts requires you to exit Form Editing mode. Switch to the standard selection or hand tool to click the buttons and test the cycle.

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. 1. Open PDF in WPS: Launch WPS Office and open your PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to view interactive form tools.
  3. 3. Add Form Elements: Insert Push Buttons, Check Boxes, Dropdowns, or Text Fields into your document as needed.
  4. 4. Edit Properties: Right-click the inserted field to access 'Properties' and customize its appearance, values, and actions.
Easily create, edit, and fill interactive PDF formsHighly compatible with standard PDF formats including Adobe Acrobat filesLightweight application with a familiar, user-friendly interfaceAll-in-one suite for PDF, Word, Excel, and Presentation tasks
microsoft office alternative - wps office

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.