PDF Editing Issues

How to Change PDF Button Color and Show/Hide Fields with JavaScript

Huda QurayshiHuda Qurayshi Nov 5, 2026 868 views

Question details

The user needs to use Acrobat JavaScript to create an interactive button that toggles its own color between black and transparent, while simultaneously showing or hiding related PDF form fields.

Change PDF Button Color and Show or Hide Fields with JavaScript
Product
PDF
Device & OS
not provided
Scenario
Creating an interactive PDF form where a user clicks a button to toggle specific form options, updating the button's appearance and revealing or hiding subsequent fields.
Observed behavior
The goal state is achieved by applying a JavaScript action to the button's Mouse Up event, updating the fill color and modifying the display properties of target fields to either hidden or visible.
Before you start

Ensure you are using a PDF editor that supports form creation and advanced JavaScript execution, such as Adobe Acrobat Pro, and verify that you know the exact names of the fields you want to show or hide.

Solution 1Recommended

Apply a Mouse Up JavaScript Action to the Button

Use a custom JavaScript script triggered by the button's Mouse Up event to check the current color state, toggle it, and update the visibility of related fields.

Interactive PDF buttons rely on Acrobat JavaScript to perform complex actions like modifying properties of multiple elements at once. By attaching the script to the 'Mouse Up' event, the changes take effect precisely when the user completes the click action.

The script will read the button's current fillColor property. Depending on its state, it assigns a new color and iterates through the target text fields to set their display property to either display.visible or display.hidden.

1
Open Button Properties

Open your PDF document in form editing mode. Right-click the button field you want to use as the toggle and select 'Properties' from the context menu.

2
Add a JavaScript Action

Navigate to the 'Actions' tab within the Properties window. Choose 'Mouse Up' from the Select Trigger dropdown menu. Then, choose 'Run a JavaScript' from the Select Action dropdown and click the 'Add' button.

3
Write the Toggle Script

In the JavaScript Editor window, write an if/else statement to check the button's current color. For example: if (event.target.fillColor === color.transparent) { event.target.fillColor = color.black; this.getField('LINE_1').display = display.visible; } else { event.target.fillColor = color.transparent; this.getField('LINE_1').display = display.hidden; }

4
Include Additional Fields

To hide or show more fields, add additional lines within both the 'if' and 'else' blocks, matching the pattern: this.getField('LINE_2').display = display.visible; (or display.hidden). Ensure the field names perfectly match those in your PDF form.

5
Save and Test

Click 'OK' to save the script, then click 'Close' on the Properties dialog. Exit form editing mode and click your button to verify that it changes color and correctly toggles the visibility of the specified fields.

Apply a Mouse Up JavaScript Action to the Button
Case Sensitivity: JavaScript in PDFs is strictly case-sensitive. Make sure you use exact capitalization for field names (e.g., 'LINE_1'), 'display.visible', and 'display.hidden'.
Powerful PDF Form Editor

Create and Manage Interactive PDF Forms with WPS Office

WPS Office provides a comprehensive PDF toolkit that lets you view, fill, and manage interactive PDF forms effortlessly. It is designed to handle standard PDF form elements natively and securely.

  1. 1. Open Your PDF: Launch WPS Office and open your interactive PDF document containing form fields.
  2. 2. Access PDF Form Tools: Navigate to the 'Form' tab in the top ribbon to access options for filling out or modifying basic form properties.
  3. 3. Interact and Save: Click on interactive buttons or text fields to fill out your form, then easily save or share the completed document securely.
Easily view, edit, and fill interactive PDF forms.Highly compatible with standard Adobe PDF formats and form fields.Lightweight and runs smoothly on Windows, Mac, and Linux.Seamlessly extract data or convert PDFs to Word, Excel, and PowerPoint.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF JavaScript not executing when I click the button?

If nothing happens when you click the button, JavaScript may be disabled in your PDF reader's security settings. In your PDF reader preferences, navigate to the JavaScript section and ensure that 'Enable Acrobat JavaScript' is checked. Additionally, verify there are no syntax errors in your script.

Can I hide multiple PDF fields with a single button click?

Yes, you can hide or show as many fields as you need. You simply add a new line referencing each specific field within the same script block, like `this.getField('LINE_2').display = display.hidden;`. Alternatively, if your fields have a sequential naming convention, you can use a loop.

What color formats are accepted in Acrobat JavaScript?

Acrobat JavaScript provides predefined color arrays such as `color.black`, `color.white`, `color.red`, and `color.transparent`. You can also define custom colors using an RGB array, formatted like `["RGB", 1, 0, 0]` for red, where the values range from 0 to 1.