How to Make a PDF Field Read-Only Based on a Checkbox
Question details
The user wants to use Acrobat JavaScript to dynamically change a text field to a read-only state when a specific checkbox is ticked in a PDF form.

- Product
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms where certain fields lock or unlock based on the user checking or unchecking a box.
- Observed behavior
- Needs a script to toggle the read-only property of a target text field dynamically depending on the checkbox's state.
Ensure you have a PDF editor that supports JavaScript execution and form field editing, and that you know the exact names of the checkbox and the target text field in your document.
Add a Custom Calculation Script to the Checkbox
Use a simple JavaScript snippet tied to the checkbox to toggle the target field's read-only property when the user interacts with it.
In interactive PDF forms, JavaScript can be triggered by mouse actions. By assigning a script to the checkbox's 'Mouse Up' action, you can instantly read its checked state and apply the read-only property to another specified field.
Enter the form editing mode in your PDF software, right-click the checkbox field you want to use as the trigger, and select 'Properties'.
In the Properties dialog, go to the 'Actions' tab. Under 'Select Trigger', choose 'Mouse Up'. Under 'Select Action', choose 'Run a JavaScript', then click 'Add'.
In the JavaScript Editor window, paste the following code: `this.getField("TextField1").readonly = event.target.isBoxChecked(0);` Make sure to replace "TextField1" with the exact name of your target text field.
Click 'OK' and close the properties window. Exit form editing mode and test your form. When you select the checkbox, the target field will become read-only; when cleared, it will be editable again.

Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to create, edit, fill out, and manage interactive PDF forms effortlessly without needing complex programming skills.
- 1. Open your PDF: Launch WPS Office and open the PDF file you wish to edit.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top ribbon menu to reveal all interactive element options.
- 3. Insert Form Elements: Click on the checkbox, text field, or button icons to insert them anywhere on your document.
- 4. Customize Properties: Double-click the inserted form fields to adjust their names, appearance, and specific properties.
- 5. Save the Document: Save your file to preserve the interactive PDF form for future users to fill out.

Frequently Asked Questions
Why isn't my JavaScript code making the field read-only?
Double-check that the field name used in `this.getField("YourFieldName")` exactly matches the actual field name, including capitalization. Also, ensure that JavaScript execution is enabled in your PDF viewer's preferences.
Can I lock multiple fields with a single checkbox?
Yes, you can apply the read-only state to multiple fields by adding multiple lines of code within the same script block. For example: `this.getField("Field1").readonly = event.target.isBoxChecked(0);` followed by `this.getField("Field2").readonly = event.target.isBoxChecked(0);`.
How do I make the field hidden instead of read-only?
You can modify the script to change the `display` property instead. Use `this.getField("TextField1").display = event.target.isBoxChecked(0) ? display.hidden : display.visible;` to hide the field when the box is checked.




