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

How to auto populate a field based on the selection in a drop down box

Community Beginner ,
Mar 13, 2024 Mar 13, 2024

I have created a form, which is nearly complete. The only thing I have left, that I'm not sure how to do is this:

 

If the selection in the [Program] dropdown box is "Biology",

then the field [Address] should be "303 McKinley Hall",

field [Phone] should be "(800) 238-4732", and

field [Email] should be "Biology@itsu.edu".

 

I can't seem to get the code right to automatically populate the Address, Phone, and Email fields based on the option chosen in [Program]. I'm probably over looking something very simple.

TOPICS
PDF forms
2.7K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Mar 13, 2024 Mar 13, 2024

As 'Validate' script of dropdown field use this:

var address = this.getField("Address");
var phone = this.getField("Phone");
var email = this.getField("Email");

if(event.value == "Biology"){
 address.value = "303 McKinley Hall";
 phone.value = "(800) 238-4732";
 email.value = "Biology@itsu.edu";}
//If you have more conditions to add addresses, phones, email add 'else if' here

else{
 address.value = "";
 phone.value = "";
 email.value = "";}

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 Expert ,
Aug 14, 2025 Aug 14, 2025
LATEST

The template field re-naming pattern is fixed, it cannot be changed. So your scripts have to be written to take this naming into account when accessing the fields. 

 

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 Expert ,
Mar 13, 2024 Mar 13, 2024

As 'Validate' script of dropdown field use this:

var address = this.getField("Address");
var phone = this.getField("Phone");
var email = this.getField("Email");

if(event.value == "Biology"){
 address.value = "303 McKinley Hall";
 phone.value = "(800) 238-4732";
 email.value = "Biology@itsu.edu";}
//If you have more conditions to add addresses, phones, email add 'else if' here

else{
 address.value = "";
 phone.value = "";
 email.value = "";}
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
New Here ,
Aug 13, 2025 Aug 13, 2025

Nesa thank you for this. I was able to make it work on my end. It works great on static form. I have a question, my PDF form has a template which I can spawn new pages if needed. The template itself works. But when I click on the button to add a page on my document the spawn page comes with a prefix of P3.page1.phone (P3 changes in reference to the page number. page1 remains the same. phone remains the same). I have explored using the var prefix but it did not work. Any idea how I can make it this work?

 

I tried using a document level java too but did not work and I am unsure why not. I think I would like to stick with my field Java if possible.

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 ,
Aug 13, 2025 Aug 13, 2025

In the script to spawn template, set field rename to false (bRename property).

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
New Here ,
Aug 13, 2025 Aug 13, 2025

Thank you, Nesa! Yes, that is an option, but when the renaming is set to false, the fields will have the same therefore whatever I enter on one page, it will duplicate on the other.

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 ,
Aug 14, 2025 Aug 14, 2025
LATEST

The template field re-naming pattern is fixed, it cannot be changed. So your scripts have to be written to take this naming into account when accessing the fields. 

 

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 Expert ,
Mar 13, 2024 Mar 13, 2024

This question is asked a lot!! 

Here are some articles that discuss list programming.

https://acrobatusers.com/tutorials/change_another_field/

https://acrobatusers.com/tutorials/list_and_combo_in_lc/

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

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 14, 2024 Mar 14, 2024

Ok, thanks for the help, but I guess I need to explain further. I have a list of 20 programs, and 5 fields to work with. I want Fields 2-5 populated based on the drop down menu choice from Field 1.

Field 1: Program

Field 2: Address

Field 3: CSZ (City/State/Sip)

Field 4: Dept Code

Field 5: Participants (number of participants)

 

Would it be easier if I program each of the four fields individually based on Field 1 rather than trying to populate all of them from Field 1?  To be honest, I haven't done this in a long time, and back then I did it with "if, else" statements, so I'm trying to wrap my head around how javascript compares to the "old way".

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 14, 2024 Mar 14, 2024

You can easily add more fields to the script I posted above (it's also using if/else).

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 15, 2024 Mar 15, 2024

Thank you again. I'm still missing something, and it is probably something very simple . . . it almost always is.  So here is a sample of my code and the results. No matter which program I choose, it will not populate the other fields. What am I missing?

 

var address = this.getField("address");

var phone = this.getField("phone");

var email = this.getField("email");

 

if(event.value == "Anthropology"){

 address.value = "541 Sampson Hall ";

 phone.value = "(555) 223-4419";

 email.value = "anthro@ist.edu";}

if(event.value == "Biology"){

 address.value = "219 Brownston Hall";

 phone.value = "(555) 223-5243";

 email.value = "biol@ist.edu ";}

if(event.value == "Communicatons"){

 address.value = "330 Marquette Hall";

 phone.value = "(555) 223-6639";

 email.value = "comm@ist.edu ";}

if(event.value == "English"){

 address.value = "411 Emerson Hall";

 phone.value = "(555) 223-7288";

 email.value = "engl@ist.edu ";}

if(event.value == "Psychology"){

 address.value = "339 Haverty Hall";

 phone.value = "(555) 223-4389";

 email.value = "psyc@ist.edu ";}

 

Elizabeth34145791s1xk_0-1710512671574.png

 

 

 

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 15, 2024 Mar 15, 2024

Script looks ok, where do you use it?

Check that field names are correct.

Can you share a file with us?

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 15, 2024 Mar 15, 2024

How do I share the file? 

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 15, 2024 Mar 15, 2024
 
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 15, 2024 Mar 15, 2024

1. Use the script in 'Validate' tab.

2. Your field name are not correct, "email" and "Email" is not same, JavaScript is case sensitive.

3. In dropdown field under 'Options' tab check 'Commit selected value immediately', if you want to show values as soon as you select program (if not checked you need to click outside the field once).

4. Added else if to the script instead of just 'if' and last else to set fields to blank.

 

Here is your file with changes made (script is in 'Validate' tab of dropdown field):

https://drive.google.com/file/d/1_4vdMaDyEBPPf0PJkH9t9urfsgwXyPGD/view?usp=sharing 

 

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