Copy link to clipboard
Copied
I have a form on which I want to make some text fields Read-Only if a certain value is selected in the drop down menu of another field. I've tried:
if (this.getField("Combo Box 9").valueAsString = "Reprint") {
this.getField("Text Field 42").readonly = true;
}
Somehow the field becomes read-only by default then.
Copy link to clipboard
Copied
So the code only changes the field to ReadOnly, not back, but it also contains an error. The code below fixes the issue.
But, since you've been trying different variations of code you may have bad code lying about in the form. Make sure all of the test code is removed. Then use this script in the "Validation" script for the "Combo Box 9" field, which is where this kind of script is supposed to go:
this.getField("Text Field 42").readonly = (event.value == "Reprint") ;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
For clarity, I've been trying a few things. I have the following as a custom format in the field I want to be readonly:
if (this.getField("eSample").item = "Reprint") {
this.getField("CoreColor").readonly = true;
}
That made the CoreColor field readonly immediately (regardless of the value in eSample). It seems like my condition isn't being read, but the action is executed anyway.
Copy link to clipboard
Copied
When comparing, use double equal signs:
if (this.getField("eSample").item == "Reprint") {
Copy link to clipboard
Copied
So the code only changes the field to ReadOnly, not back, but it also contains an error. The code below fixes the issue.
But, since you've been trying different variations of code you may have bad code lying about in the form. Make sure all of the test code is removed. Then use this script in the "Validation" script for the "Combo Box 9" field, which is where this kind of script is supposed to go:
this.getField("Text Field 42").readonly = (event.value == "Reprint") ;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
@Thom Parker is there a way to target more than one field? I tried this.getField("Text Field 42", "Text Field 43").readonly = (event.value == "Reprint") ; but it didn't work.
Copy link to clipboard
Copied
There are two ways to handle multiple fields.
1) Group the fields using a group name prefix on the field names.
ex: "Text.Field32", "Text.Field33"
then this code will set the readonly property for all fields in the group.
this.getField("Text").readonly = ....
2) Set each field individually
ex:
var bReadOnly = (event.value == "Reprint") ;
this.getField("Text Field 42").readonly = bReadOnly;
this.getField("Text Field 43").readonly = bReadOnly;
this.getField("Text Field 44").readonly = bReadOnly;
Use the Acrobat JavaScript Reference early and often

