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

Javascript to have one field to autofill from dropdown box choice

New Here ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

651

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 ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

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 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
New Here ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

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?

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 ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

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 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
Explorer ,
Aug 07, 2020 Aug 07, 2020

Copy link to clipboard

Copied

Hi Thom,

 

I added your script to my form.  It worked but when I select a different option, the value in "Text" remains at N/A instead of a blank text.  How can I make it that option 1 would export N/A, while the other options would export a blank text. 

 

Thank you for your help!

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 ,
Aug 07, 2020 Aug 07, 2020

Copy link to clipboard

Copied

Change this part of it:

 

else

oFld.readonly = false;

 

To:

 

else {

oFld.readonly = false;

oFld.value = "";

}

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
Explorer ,
Aug 07, 2020 Aug 07, 2020

Copy link to clipboard

Copied

LATEST

Thanks try67 and Thom for the scripts

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