Skip to main content
Participant
February 6, 2025
Answered

Button to lock and unlock all form fields

  • February 6, 2025
  • 2 replies
  • 1453 views

Hello I created a button to lock all fields once pressed with the script.  When I press  it nothing happens.

 

// Lock all fields in the form
var allFields = this.getFieldNames();
for (var i = 0; i < allFields.length; i++) {
this.getField(allFields[i]).readonly = true;
}

 

Can someone help.? I would also need to unlock to make edits.

 

Thank you.

Correct answer PDF Automation Station

Change the word true in the script to false.  You would have to exclude the unlocking button field from the original script or else it would be locked ("readonly") and wouldn't work.

2 replies

PDF Automation Station
Community Expert
Community Expert
February 6, 2025

this.getFieldNames() is not a function.  Try this:

for(var i = 0; i < this.numFields; i++)
{
var fieldName = this.getNthFieldName(i);
var oFld=this.getField(fieldName);
if(oFld==null){continue}
oFld.readonly=true;
}
tony_6388Author
Participant
February 6, 2025

That worked for locking thank you!!!!

If I wanted to put a button to unlock the fields, to make changes - what would I change?

 

Thank you.

PDF Automation Station
Community Expert
Community Expert
February 6, 2025

Change the word true in the script to false.  You would have to exclude the unlocking button field from the original script or else it would be locked ("readonly") and wouldn't work.

Bernd Alheit
Community Expert
Community Expert
February 6, 2025

Check the Javascript console for errors. getFieldNames is not a function of Acrobat.