How to Use JavaScript to Change Multiple Button Colors in PDF
Question details
The user wants to configure a PDF form so that clicking one button changes its own color as well as the colors of several related buttons simultaneously using JavaScript.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Customizing interactive PDF forms to provide immediate visual feedback when specific buttons are clicked by the user.
- Observed behavior
- The user needs a script to target multiple form fields and update their background colors simultaneously upon a click event.
Ensure your PDF editor supports JavaScript execution in forms and that you have noted the exact field names of the buttons you intend to modify.
Use the fillColor Property via JavaScript
Apply a custom JavaScript action to your trigger button to dynamically change the background color of multiple fields.
You can dynamically alter the background color of any PDF form field by accessing its fillColor property via JavaScript. Note that PDF JavaScript uses a 0 to 1 scale for RGB values, not the traditional 0 to 255 scale.
Open your PDF document in your PDF editor and enter the Form Editing mode to reveal all interactive fields.
Right-click the button that will act as the trigger (the one the user clicks) and select Properties, then navigate to the Actions tab.
Choose 'Run a JavaScript' from the Select Action dropdown menu and click the Add button to open the script editor.
Enter the script to change the target buttons' colors. For example: this.getField("TargetButton1").fillColor = ["RGB", 1, 0.65, 0]; Repeat this line for each button you want to change, replacing 'TargetButton1' with the respective field names.
Click OK to save the script, exit form editing mode, and click your trigger button to test the color changes.

Easily Create and Edit Interactive PDFs with WPS Office
WPS Office provides a comprehensive PDF editor that allows you to effortlessly manage, create, and customize interactive PDF forms. Enjoy advanced form-editing capabilities in a seamless, lightweight interface.
- 1. Open PDF in WPS Office: Launch WPS Office and open the PDF document you wish to edit.
- 2. Navigate to the Form tab: Click on the 'Form' tab on the top ribbon to access a full suite of interactive form controls.
- 3. Add or modify buttons: Insert new push buttons, text fields, or checkboxes, and double-click them to modify their appearance and properties.
- 4. Save your interactive document: Once your form is fully customized, save the document to preserve all field properties and interactivity.

Frequently Asked Questions
Why is my PDF JavaScript color not displaying correctly?
PDF JavaScript requires RGB values to be on a scale of 0 to 1. If you enter standard values like 255, the color will default to an incorrect shade. Ensure you divide your standard RGB values by 255 before entering them into the script.
Can I change the text color of the button using JavaScript?
Yes, you can change the text color by targeting the textColor property of the field instead of the fillColor property, using the exact same RGB array format.
How do I reset the button color back to its original state?
You can create another button or assign an additional action that sets the fillColor property to color.transparent, or manually specify the array of your original RGB values.




