Skip to main content
sw777
Known Participant
March 29, 2017
Answered

Populating textboxes based on content in dropdown boxes

  • March 29, 2017
  • 1 reply
  • 8970 views

I'm pretty new to JavaScript so I have very little idea how to achieve this. But I'm learning. 🙂

I have three (3) dropdown boxes on a PDF form where the user can select a series of courses. All dropdown boxes contain the same list of courses, for example...

Course A - Design widgets

Course B - Assemble widgets

Course C - Test widgets

Course D - Paint widgets

Course E - Pack widgets

When a selection is made in one of the dropdowns (Course1) an adjacent textbox (NominalHours_Course1) needs to display the number of hours that the course typically runs for (e.g. 5, 8, 12, 15, etc.). This is a predetermined value for each course and needs to be retrieved from a table... (I assume stored somewhere in the document?).

I'm after the same sort of functionality as the MS Excel VLOOKUP function.

Reading some of the posts on this forum, it looks like the relationship between course and hours should be in a document level script, allowing the "Nominal Hours" values to be referenced by any of the dropdown boxes and textboxes. This would also make it easier to change values if required since the changes would only need to be made in the one location.

I have no idea where to start with this. I have tried working through the JavaScript Tutorials on w3schools.com but I think I might be trying to bite off more then I can chew (being such a beginner at JavaScript).

Hoping someone can help.

This topic has been closed for replies.
Correct answer try67

It is unlikely that I will need to change anything on a regular basis so I think the first option would be best.

If you wouldn't mind providing the code (with an explanation of what each part does so I can learn this stuff) it would be greatly appreciated.

Thanks.


Sure. OK, so you can place this code as your doc-level script:

var courseData = {

"Course A - Design widgets" : 5,

"Course B - Assemble widgets" : 8,

"Course C - Test widgets" : 12,

"Course D - Paint widgets" : 15,

"Course E - Pack widgets" : 21

};

function calcCourseHours(dropdownFieldName) {

    var v = courseData[this.getField(dropdownFieldName).value];

    if (v) event.value = v;

    else event.value = "";

}

And then as the custom calculation of your text field enter this code:

calcCourseHours("Course1");

Pretty straight-forward...

1 reply

try67
Community Expert
Community Expert
April 20, 2017

There are a couple of ways of doing it.

You can hard-code the values into your code and then refer to them with your script, or you can use a text file that contains this data, attach it to the PDF and then parse it when the file is opened and use the information from it in your code.

The former is easier to set up, but a bit trickier to maintain, as it requires editing the actual code. The latter is a bit more complicated to set up, but once it's working all you need to do in order to update the values is to open that attached text file, edit it and save it, and you're done.

I can explain how to do the former, if you wish.

You can do the latter using this tool I've developed, with some additional code I can help you with: Custom-made Adobe Scripts: Acrobat -- Import Items from a Text File to a Combo-Box or List Field

sw777
sw777Author
Known Participant
April 20, 2017

The form needs to be distributed via email so I assume it would be better not to have a separate text file associated with it, or am I missing what you mean by "attach it to the PDF"?

try67
Community Expert
Community Expert
April 20, 2017

It will be a part of the PDF file. You will only need to distribute one

file.