How to Create a Toggle Color Button in Acrobat PDF
Question details
The user wants to configure a button in a PDF form that dynamically toggles its background fill color between yellow and red every time it is clicked.

- Product
- Adobe Acrobat PDF
- Device & OS
- not provided
- Scenario
- Adding interactive elements to a PDF form to visually indicate button states using JavaScript.
- Observed behavior
- The button needs to change its background color to the opposite color (yellow to red, or red to yellow) on the Mouse Up event, and a text field requires inverse RGB colors based on numeric values.
Ensure you have the 'Prepare Form' tool enabled in Adobe Acrobat and that JavaScript execution is permitted in your PDF viewer's security preferences.
Add JavaScript to Toggle Colors on Mouse Up
Use this solution to directly apply a conditional color-changing script to the button's click event.
Acrobat allows you to attach custom JavaScript snippets to form fields. By using a simple ternary operator in the Mouse Up event, the button can evaluate its current fill color and switch to the alternative color.
Launch your document in Adobe Acrobat, navigate to 'Tools', and select 'Prepare Form' to enter form editing mode.
Right-click the button field you want to modify and select 'Properties' from the context menu.
Go to the 'Actions' tab. Select 'Mouse Up' in the Select Trigger dropdown menu, choose 'Run a JavaScript' in the Select Action menu, and click the 'Add...' button.
In the JavaScript Editor, type or paste the following code: event.target.fillColor = color.equal(color.yellow, event.target.fillColor) ? color.red : color.yellow; Click 'OK' to save.

Set Inverse Fill and Text Colors Based on Field Value
Use this solution if you need a text field to dynamically adjust its fill and text colors based on numerical input (e.g., creating high-contrast inverse colors).
Manage and Fill Interactive PDF Forms with WPS Office
WPS Office provides a highly compatible and lightweight PDF editor. It easily opens and allows you to fill out interactive PDF forms without requiring expensive premium software, providing a smooth user experience.
- 1. Download WPS Office: Download and install the free WPS Office suite from the official website.
- 2. Open Your Interactive PDF: Launch WPS PDF and open your interactive PDF document containing the dynamic form buttons.
- 3. Fill and Manage the Form: Navigate to the 'Form' tab to interact with the fields, click buttons, and view dynamic color changes smoothly.

Frequently Asked Questions
Can I use colors other than yellow and red for my PDF button?
Yes, Adobe Acrobat JavaScript supports several predefined colors such as color.blue, color.green, color.black, and color.white. You can replace color.yellow and color.red in your script with any of these predefined options.
Why isn't my JavaScript button changing colors when I click it?
This usually happens if you are still in 'Prepare Form' editing mode. You must switch to 'Preview' mode or close the form editing tools to interact with the button. Additionally, check that JavaScript is enabled in your PDF viewer's preferences.
How do I define a custom RGB color in Acrobat JavaScript?
A custom RGB color is defined as an array where the first element is the color space ('RGB') followed by three normalized values between 0 and 1. For example, a custom purple would be defined as ['RGB', 0.5, 0, 0.5].
Will this JavaScript color toggle work in all PDF viewers?
No, not all third-party PDF viewers or web browser PDF plugins support Acrobat JavaScript actions. For the scripts to work as intended, the document should be opened in robust PDF applications like Adobe Acrobat or highly compatible readers.




