Highlighted

Javascript to have one field to autofill from dropdown box choice

New Here ,
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, How to, PDF forms

Views

50

Likes

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

Javascript to have one field to autofill from dropdown box choice

New Here ,
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, How to, PDF forms

Views

51

Likes

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
Adobe Community Professional ,
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;

 

Likes

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
Reply
Loading...
New Here ,
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?

Likes

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
Reply
Loading...
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Explorer ,
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!

Likes

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
Reply
Loading...
Most Valuable Participant ,
Aug 07, 2020

Copy link to clipboard

Copied

Change this part of it:

 

else

oFld.readonly = false;

 

To:

 

else {

oFld.readonly = false;

oFld.value = "";

}

Likes

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
Reply
Loading...
thuyviii LATEST
Explorer ,
Aug 07, 2020

Copy link to clipboard

Copied

Thanks try67 and Thom for the scripts

Likes

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
Reply
Loading...
Resources
One Stop Solution for Acrobat
Edit a PDF