PDF Editing Issues

How to Change PDF Form Field Color on Focus and Blur with JavaScript

Emma BrownEmma Brown Nov 3, 2026 869 views

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.

How to Change PDF Field Color on Focus and Blur with 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.
Before you start

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.

Solution 1Recommended

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.

1
Access Form Field Properties

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.

2
Add the On Focus Action

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.

3
Input the Highlight Code

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'.

4
Add the On Blur Action

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'.

5
Save and Test the Form

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.

Configure On Focus and On Blur JavaScript Actions
Standard Color Variables: Standard preset colors you can use in Acrobat JavaScript include `color.transparent`, `color.black`, `color.white`, `color.red`, `color.green`, `color.blue`, `color.cyan`, `color.magenta`, and `color.yellow`.
Powerful PDF Form Editor

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. 1. Open the PDF File: Launch WPS Office and open your target PDF document. Navigate to the 'Form' tab on the top ribbon.
  2. 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. 3. Edit Field Properties: Double-click the selected field to open its Properties dialog, where you can customize its appearance, format, and behavior.
  4. 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.
Easily insert, edit, and align PDF form fieldsSupport for advanced form actions and property customizationFully compatible with Adobe Acrobat PDF formats and interactive elementsLightweight, fast, and features a familiar user-friendly interface
microsoft office alternative - wps office

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.