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

Change form field to editable/non editable based on drop down selection

Community Beginner ,
Feb 05, 2024 Feb 05, 2024

Copy link to clipboard

Copied

Hi all,

 

I am creating a fillable form to collect data, however two of the selectable fields require additional information.

 

I have created the page with the following fields:

 

Selection.jpg

The drop down includes 'Companies House' and 'Office for Students'. When either of these are selected, the wording on the left of the text box changes to ask for the required number. All other selections should be N/A.

 

The issue I have is trying to make the text box be uneditable and not required when N/A is showing and editable/required when 'Companies House' or Office for Students (OFS)' is selected.

 

The code I am using in the dropdown is:

var number = this.getField("Company/OFS Number");
if(event.value == "Companies House")
number.value = "";
else if(event.value == "Office for Students (OFS)")
number.value = "";
else number.value = "N/A";

 

If I add 'number.readonly = true;' to the end, it locks the form and adding 'number.readonly = false;' gives an error.

 

Any assistance in resolving this would be most welcome.

 

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

140

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Beginner , Feb 06, 2024 Feb 06, 2024

Hi Thom,

 

Thank you for your reply.

 

I am sorry if my post was not fully complete and I used the incorect naming convention. I will definitely take your comments onboard for any future posts I make and also for my general learning of the conventions to use.

 

I added the code and this worked fine for 'Companies House' but was not working for the 'OFS' field. This made me read the code in order to understand it better. I then found that I need to  change cOFSNum .value to event.value

 

else if(cOFSNum

...

Votes

Translate

Translate
Community Expert ,
Feb 05, 2024 Feb 05, 2024

Copy link to clipboard

Copied

So first, "number" is not a good name for a variable, since it's also the name of  a type in JS. 

Try this code:

 

var cOFSNum = this.getField("Company/OFS Number");
cOFSNum.readonly = false;

if(event.value == "Companies House")
   cOFSNum.value = "";
else if(event.value == "Office for Students (OFS)")
   cOFSNum.value = "";
else{
   cOFSNum.value = "N/A";
   cOFSNum.readonly = true;
}

 

 

Please be specific in your comments. If there is an error, don't just say there is an error. Tell us what error, where and how is it reported? When does it happen? 

A phrase like "locks the form" doesn't mean anything to us. Are all the form fields on the form uneditable? Or just one form field. Be specific.

 

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

Votes

Translate

Translate

Report

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 ,
Feb 06, 2024 Feb 06, 2024

Copy link to clipboard

Copied

Hi Thom,

 

Thank you for your reply.

 

I am sorry if my post was not fully complete and I used the incorect naming convention. I will definitely take your comments onboard for any future posts I make and also for my general learning of the conventions to use.

 

I added the code and this worked fine for 'Companies House' but was not working for the 'OFS' field. This made me read the code in order to understand it better. I then found that I need to  change cOFSNum .value to event.value

 

else if(cOFSNum .value == "Office for Students (OFS)")

 

Everything is now working correctly and as intended.

 

Thank you so much fo your assistance with this.

Votes

Translate

Translate

Report

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 ,
Feb 06, 2024 Feb 06, 2024

Copy link to clipboard

Copied

LATEST

Excellent Catch!! I fixed it in the post. 

 

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

Votes

Translate

Translate

Report

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