Skip to main content
Participant
March 13, 2024
Answered

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

  • March 13, 2024
  • 4 replies
  • 2760 views

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.

Correct answer Thom Parker

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.


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. 

 

4 replies

Participant
March 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 ";}

 

 

 

 

Nesa Nurani
Community Expert
Community Expert
March 15, 2024

Script looks ok, where do you use it?

Check that field names are correct.

Can you share a file with us?

Participant
March 15, 2024

How do I share the file? 

Participant
March 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".

Nesa Nurani
Community Expert
Community Expert
March 14, 2024

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

Thom Parker
Community Expert
Community Expert
March 13, 2024
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Nesa Nurani
Community Expert
Community Expert
March 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 = "";}
Participant
August 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.

Nesa Nurani
Community Expert
Community Expert
August 13, 2025

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