Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
5

Make text fields read only based on content selected in a drop down

Community Beginner ,
Mar 27, 2024 Mar 27, 2024

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. 

TOPICS
JavaScript , PDF forms
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 27, 2024 Mar 27, 2024

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") ;

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 27, 2024 Mar 27, 2024

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2024 Mar 27, 2024

When comparing, use double equal signs:

if (this.getField("eSample").item == "Reprint") {

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2024 Mar 27, 2024

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") ;

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 27, 2024 Mar 27, 2024

@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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2024 Mar 27, 2024
LATEST

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;

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines