Skip to main content
Participating Frequently
August 27, 2024
Answered

How to change properties for all form fields starting with the same name at the same time?

  • August 27, 2024
  • 2 replies
  • 1066 views

I am working on a travel form which has 3 different sections: advance, actual, and reconcile. Each section will have it's own set of fields.

I would like to create a button which will set all the advance fields to read only at the same time. Fields are named advance.start_date, advance.end_date, reconcile.total, etc.

Essentially when I click a button to set the advance, it will change all fields starting with advance to read only. I will do the same to set the actual.

The goal is to make the form so that the user can't change the advance details after once committed. I realize that read only can be undone by editing the form setup and unchecking read only.

 

Essentially I know I need a javascript to do the following:

  1. create an array of fields with the name starting with advance.
  2. loop through the array 
  3. extract the field name
  4. execute this.getField("fieldName").readonly = true;
  5. exit the loop
This topic has been closed for replies.
Correct answer PDF Automation Station

Actually, an easier way would be to loop through all fields and test whether the field name contains "advance".  This is done by testing the regular expression "advance".  You can also speed up the loop and avoid errors for testing for the type of field.  Assuming it is a text field, here's the script:

for(var i = this.numFields - 1; i > -1; i--)
{
var oFld = this.getNthFieldName(i);
if(this.getField(oFld).type=="text" && /advance/.test(oFld))
{
this.getField(oFld).readonly=true;
}
}

2 replies

try67
Community Expert
Community Expert
August 27, 2024

Since they are all part of a group called "adnvace", this is all you need to do that:

this.getField("advance").readonly = true;

 

As for locking the fields, you can use a Signature field to do it securely. Under its Properties (Signed tab) you can set it to lock some fields, while leaving others as editable.

Participating Frequently
August 27, 2024

I don't really want to lock the fields until later in the process. When I do this I'll actually flatten the document. I'm just wanting to set fields to read only so they can not be easily edited as the form is going through the process. 

PDF Automation Station
Community Expert
Community Expert
August 27, 2024

Actually, an easier way would be to loop through all fields and test whether the field name contains "advance".  This is done by testing the regular expression "advance".  You can also speed up the loop and avoid errors for testing for the type of field.  Assuming it is a text field, here's the script:

for(var i = this.numFields - 1; i > -1; i--)
{
var oFld = this.getNthFieldName(i);
if(this.getField(oFld).type=="text" && /advance/.test(oFld))
{
this.getField(oFld).readonly=true;
}
}