PDF Editing Issues

How to Unlock Read-Only PDF Form Fields with JavaScript

Huma Ashraf ChHuma Ashraf Ch Nov 3, 2026 869 views

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.

How to Unlock Read-Only PDF Form Fields Using a JavaScript Button
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.
Before you start

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.

Solution 1Recommended

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.

1
Access the Button Properties

Right-click the button field in your PDF form editor, select 'Properties', and navigate to the 'Actions' tab.

2
Edit the JavaScript Action

Select 'Run a JavaScript' from the actions list and click 'Edit' to open the JavaScript editor window.

3
Correct the Password Comparison

Ensure you are comparing the variable directly to your password string. For example: if (z == 'Password1').

4
Target Fields Individually

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; }

5
Save and Test

Click 'OK' to save the script, exit form editing mode, and click the button to verify the fields become editable.

Fix the Password Variable Comparison in Your JavaScript
JavaScript Console Validation: If the button still does not work, press Ctrl+J (or Cmd+J on Mac) in Adobe Acrobat or your advanced PDF viewer to open the JavaScript console and check for syntax errors.
Advanced PDF Form Management

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. 1. Open your PDF: Launch WPS Office and open your interactive PDF document.
  2. 2. Navigate to the Form Tab: Click on the 'Form' tab in the top ribbon to access all interactive form controls.
  3. 3. Edit Form Properties: Double-click any form field to open its properties dialog.
  4. 4. Manage Read-Only Settings: In the properties menu, easily toggle the 'Read-only' checkbox to lock or unlock fields manually without scripting.
Easily add text fields, checkboxes, and buttons to interactive PDFsManage document security, permissions, and read-only states seamlesslyFully compatible with standard PDF formats including Adobe Acrobat formsLightweight, fast-loading, and user-friendly cross-platform interface
microsoft office alternative - wps office

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.