Skip to main content
Inspiring
December 14, 2017
Answered

multiple drop down auto populate diffrent field?

  • December 14, 2017
  • 3 replies
  • 1534 views

I am currently working on a project where I have 6 different dropdown lists all with the same information that would auto populate the corresponding field next to them.

I obviously am doing something wrong because I can not get each dropdown list and corresponding field to act on their own. What I end up with is being able to control only 1 at a time or all of them. If you look at my small sample code below maybe you can see what I am doing wrong. Any help would be greatly appreciated.

var DepData={"Code Blue Elevator":{contact: "Your Department Chief"},

                       "Lab":{conatact: "Your Department Chief"},

                        };

                         function SetFieldValues(cDeptName)

                         {

                         this.getField("DeptContact").value = DeptData[cDeptName].contact;

                         }

[Edited by Mod.: Code layout fixed]

This topic has been closed for replies.
Correct answer jonathanc1264

I figured it out.... see below if anyone else ever has this issue.

So for each dropdown box I added the following code below to the format tab "Create and Edit JavaScripts" changing drobox1 to dropbox2 and text1 to text 2 and so on and so forth. I also named it dropbox1 and text1 so it is easy for someone to understand what is what.

event.value = this.getField ("dropbox1").value;

if (!event.willCommit)

{this.getField ("text1".value=event.changeEX;}

3 replies

jonathanc1264AuthorCorrect answer
Inspiring
December 19, 2017

I figured it out.... see below if anyone else ever has this issue.

So for each dropdown box I added the following code below to the format tab "Create and Edit JavaScripts" changing drobox1 to dropbox2 and text1 to text 2 and so on and so forth. I also named it dropbox1 and text1 so it is easy for someone to understand what is what.

event.value = this.getField ("dropbox1").value;

if (!event.willCommit)

{this.getField ("text1".value=event.changeEX;}

Thom Parker
Community Expert
Community Expert
December 19, 2017

You say you put this in the Format event? Or do you mean the Keystroke event.

So the first line of code does nothing.  It sets the appearance value of the drop-down to its own value.

The second line only has meaning for a keystroke event, which is what you must be talking about since you're using the event.changeEx property which only has value in the Keystroke event.

So what you are saying is that you hand coded each field to do what I was telling you to do in the previous posts.

For other people read this, do not use the code posted by jonathanc1246, this is not the right way to go.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
December 21, 2017

Thom Parker,

I'm not sure you picked up on this but I do not write code for a living... I had a task that needed completing so I reached out on this forum hoping for a solution to the problem I had. Please do not take your inability to explain in a helpful way as any kind of insult to your intelligence. I literally scoured the internet for a solution to my problem. In my mind if it works then there is no "wrong" way to go. Maybe you should make another video just for the purpose of explaining why my solution to the problem is not the right way to go and I would happily watch it. I love to learn and to help others so when you respond to the next person to try and help please keep in mind that their working knowledge is probably not at your level hence the reason they are reaching out for assistance.

Thom Parker
Community Expert
Community Expert
December 14, 2017

In the SetFieldValues() function, the target field name is hard coded. This needs to be set differently for each dropdown, because they each set a different text field.

There are a couple of ways to do this.

1. Include the target field name as a function input

2. Automatically generate the target field name from the name of the current field.

I like #2 best because it reduces the amount of work I have to do. but it requires the fields and named in a consistent fashion that lends itself to this process.  For example "DropDown1" goes to "TextField1". 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
December 14, 2017

I'm not the best at script but I think I understand. is there any way you could give me an example of how to write that in code?

Thom Parker
Community Expert
Community Expert
December 14, 2017

The easy method is to just include the name in the code

Here is a redo of your set function

                        function SetFieldValues(cDeptName, cTargetField)

                        {

                        this.getField(cTargetField).value = DeptData[cDeptName].contact;

                        }

Now you're Commit (keystroke) script on the dropdown should include something like this

SetFieldValues(event.value, "TextField1");

Of course the "TextField1" needs to actually be the name of the target field on your form.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
December 14, 2017

Is "conatact:" correct?

Inspiring
December 14, 2017

no sorry just a typo couldn't copy and paste the script so I re typed some of it.