Skip to main content
Lyle_Worsley
Participating Frequently
April 13, 2020
Question

Javascript to have one field to autofill from dropdown box choice

  • April 13, 2020
  • 1 reply
  • 1458 views

Loking for the javascript todo the following:

 

I would like a field to autofill the word N/A or allow text in a field depending on the choice from a dropdown box.  The dropdown box has 5 choices and each has a numeric value from 1 to 5. 

Example.  If the choice from the dropbox is the 2nd choice, I want the field to automatically enter N/A in the field. If Choice 3 is selected, I want the ability to enter plain text.

 

Thanks for the help.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
April 13, 2020

Use a custom Validate Script on the dropdown.  Here's a sample script. You didn't specify what happens on the other selections

Note, that for the validate event, the "if" test is on the display value, not the export value.

 

var oFld = this.getField("Text");

if(event.value == "2nd Choice")

{

     oFld.value = "N/A";

     oFld.readonly = true;

}

else

     oFld.readonly = false;

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Lyle_Worsley
Participating Frequently
April 13, 2020

Thanks for the response.

 

What I am trying to do is have one field check the value of the dropdown box selection and based on that selction, have the output either be N/A or no entry in the field so that any text can be typed.  I was thinking it would have to have an if/else statement such as:

 

the Dropdown box name is WorkToBePerformed

 

if choice 1  then ""

else choice 2 then ""

else choice 3 then N/A

else choice 4 then N/A

else choice 5 then N/A

 

Does this make sense to you?

Thom Parker
Community Expert
Community Expert
April 13, 2020

So, the only way to have one field check the value of another (when that other field changes) is to use a calculation script. It is a really bad idea to have a field show both a calculated value and allow user entry.  There are all kinds of issues surronding calculations that can easily cause problems.  

 

The best solution in any case where one field is controling another, is to put the controlling script onto the field that does the controlling. In this case it's the dropdown. Use the script I provided above.

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