How to Change PDF Form Field Color on Focus and Blur with JavaScript
Question details
The user needs to dynamically alter the fill color of a PDF form field when it is selected (focused) and revert it back to normal when unselected (blurred) using JavaScript.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Designing an interactive PDF form where active input fields are visually highlighted to guide users as they fill out the document.
- Observed behavior
- The form field should turn blue (or another specified color) upon receiving focus and return to white or transparent when the user clicks away.
Ensure that your PDF editing software supports JavaScript for form fields, and that you have access to the form field properties panel in your editing environment.
Configure On Focus and On Blur JavaScript Actions
Apply simple Acrobat JavaScript commands to the field's action triggers to dynamically modify its background color property.
By utilizing the `event.target.fillColor` property in a PDF's JavaScript settings, you can create a highly interactive and user-friendly form. Highlighting the currently active field helps visually guide the user through complex documents.
Enter the form editing mode in your PDF editor. Right-click the specific text field you want to modify and select 'Properties' from the context menu.
Navigate to the 'Actions' tab within the Properties window. Set the 'Select Trigger' dropdown menu to 'On Focus'. Choose 'Run a JavaScript' from the 'Select Action' menu, and click the 'Add' button.
In the JavaScript Editor window that appears, type `event.target.fillColor = color.blue;` (you can change 'blue' to your desired standard color) and click 'OK'.
Back in the 'Actions' tab, change the 'Select Trigger' dropdown to 'On Blur'. Add another 'Run a JavaScript' action. In the editor, type `event.target.fillColor = color.white;` (or use `color.transparent`) to revert the color when the field loses focus, and click 'OK'.
Click 'Close' to exit the properties window. Exit the form editing mode and click inside the field to verify that the background color changes on focus and reverts when you click elsewhere.

Create and Customize Interactive Forms with WPS PDF
WPS Office provides a comprehensive suite of PDF editing tools that allow you to easily create interactive forms, add text fields, and manage advanced properties. Whether you are adding JavaScript behaviors or simply designing a layout, WPS PDF makes the process seamless.
- 1. Open the PDF File: Launch WPS Office and open your target PDF document. Navigate to the 'Form' tab on the top ribbon.
- 2. Insert or Select a Field: Click 'Text Field' to draw a new form field, or click 'Edit Form' to select an existing interactive field in the document.
- 3. Edit Field Properties: Double-click the selected field to open its Properties dialog, where you can customize its appearance, format, and behavior.
- 4. Manage Actions: Switch to the 'Actions' tab to add triggers like On Focus and On Blur, allowing you to enhance your form's interactivity seamlessly.

Frequently Asked Questions
Can I use custom RGB colors instead of the standard preset color names?
Yes. In Acrobat JavaScript, you can define custom colors using an array format. Instead of `color.blue`, use `['RGB', r, g, b]` where the RGB values are decimals between 0 and 1. For example, `['RGB', 0.8, 0.9, 1]` creates a light blue shade.
Why isn't the field color changing when I click on it to test?
Ensure you have fully exited form editing mode, as actions typically do not trigger while the form is actively being designed. Additionally, verify that JavaScript execution is enabled in your PDF viewer's preference settings.
Will these JavaScript color changes work on mobile devices or web browsers?
JavaScript execution is highly dependent on the PDF viewer being used. While robust desktop editors like WPS Office and Adobe Acrobat fully support it, many built-in web browser viewers and default mobile PDF apps have limited support for form JavaScript, which may prevent the dynamic color changes from functioning.




