How to Unlock Read-Only PDF Form Fields with JavaScript
Question details
The user needs to make read-only PDF form fields editable using a button and a password input field, but the JavaScript comparison logic is failing.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where specific fields (like Exit.date and Exit.code) become editable only when the correct password is provided and a button is clicked.
- Observed behavior
- The JavaScript password comparison does not trigger correctly, leaving the target fields as read-only even after entering the password.
Make sure your PDF editor supports JavaScript execution for form fields, and test your script on a duplicate copy of your file to prevent accidental form corruption.
Fix the Password Variable Comparison in Your JavaScript
Correct the conditional statement in your button's JavaScript action to compare the password directly and explicitly set the read-only property for individual child fields.
When referencing a field value stored in a variable (e.g., variable 'z'), the variable already represents the string value. Accessing redundant properties on it will cause the script to fail. Additionally, setting properties on a parent field name (like 'Exit') might not always cascade perfectly to child fields (like 'Exit.date' and 'Exit.code') depending on the PDF viewer.
Right-click the button field in your PDF form editor, select 'Properties', and navigate to the 'Actions' tab.
Select 'Run a JavaScript' from the actions list and click 'Edit' to open the JavaScript editor window.
Ensure you are comparing the variable directly to your password string. For example: if (z == 'Password1').
Update the script to toggle the readonly status for each specific field. Add the following logic: if (z == 'Password1') { this.getField('Exit.date').readonly = false; this.getField('Exit.code').readonly = false; } else { this.getField('Exit.date').readonly = true; this.getField('Exit.code').readonly = true; }
Click 'OK' to save the script, exit form editing mode, and click the button to verify the fields become editable.

Easily Manage Interactive PDF Forms with WPS Office
WPS Office provides an intuitive, comprehensive PDF toolkit that lets you design interactive forms, manage field properties, and secure your documents effortlessly without requiring complex coding.
- 1. Open your PDF: Launch WPS Office and open your interactive PDF document.
- 2. Navigate to the Form Tab: Click on the 'Form' tab in the top ribbon to access all interactive form controls.
- 3. Edit Form Properties: Double-click any form field to open its properties dialog.
- 4. Manage Read-Only Settings: In the properties menu, easily toggle the 'Read-only' checkbox to lock or unlock fields manually without scripting.

Frequently Asked Questions
Why is my JavaScript button not working in some PDF viewers?
Not all PDF viewers support JavaScript execution. Browsers' built-in PDF viewers (like Chrome or Edge) often restrict or ignore embedded scripts. For form scripts to work reliably, users should open the PDF in dedicated software like WPS Office or Adobe Acrobat.
How do I make all PDF fields read-only automatically?
You can add a document-level JavaScript that loops through all fields in the document and sets their readonly property to true upon saving or closing, or use standard PDF security settings to restrict editing.
Can I lock PDF fields immediately after a user signs it?
Yes. When configuring a digital signature field, you can set an action in the properties menu to automatically mark specific fields, or the entire document, as read-only as soon as the signature is applied.




