PDF JavaScript and ComboBox fields

I'm building a new PDF invoice form.
I want to be able to have the user select an item from a Product combo-box and have it fill in two additional fields (one of which is a combo-box and the other is a text field) based on what is selected in the first field. I have to use Adobe Acrobat JavaScript to handle this.

What's the best approach?

Here's what I'm thinking so far (haven't laid out anything yet, this is still in the design phase):
1. Create a JavaScript array in a button to populate the Product field with both an item and an export value for that item. This lets me maintain one list to populate all Product fields from.
2. Create a JavaScript function that triggers on change on the Product field. This would have to set the value of the second drop down (Product Source combo-box) as well as the Item Number (text field). It could draw either the item or export value from the Product combo-box. The Product Source (second combo-box) needs to be a combo-box as there will be multiple sources for each product. The part number will stay the same regardless, hence the need for a text-box instead of a combo-box.

What I'm looking for (for you to receive the points) is a working example:
Drop-down list that triggers an update of the contents of a second drop-down list and a textbox.

Note to standard JavaScript developers who aren't familiar with Adobe Acrobat JavaScript - there are differences. Not all code that will work in a web browser will work in a PDF file. Please test your code before sending me on a wild goose chase :) I don't really like chasing wild geese.
Who is Participating?
Karl Heinz KremerCommented:
It sounds like you are already familiar enough with Acrobat's JavaScript to tackle this (you know about the difference between item value and export value for example).

Here is how I would structure it: All the JavaScript functions that get called by your UI elements would be on the document level. This way, you can then keep the actual handler code for the onSelect or onClick events relatively small.

There is one trick you need to know: Usually with a list box or a combo box, your action script is executed only when the field loses focus. You don't want that, because the user would have to tab out of the control in order to have the second combo box populated: On the properties dialog, select the Options tab and make sure that "Commit selected value immediately" is selected.

Create a custom validation script. Just keep it simple and enter the following into the edit field:

Now you need to create that function. Select Advanced>Document Processing>Document JavaScripts...
Create a new script named UpdateComboBox - use the following code:

function UpdateComboBox(val)
    var box2 = new Array();
    var text = "";
    if (val == "Item1")
        box2 = ["One", "Two", "Three"];
        text = "Text1";
    else if (val == "Item2")
       box2 = ["AAAA", "BBBB", "CCCC"];
        text = "Text2";
    else if (val == "Item3")
       box2 = ["I", "II", "III", "IV", "V"];
        text = "Text3";
    else if (val = "Item 4")
       box2 = ["Four", "Three", "Two", "One"];
        text = "Text 4";
        // ...
    this.getField("Text1").value = text;

Open in new window

bhlangAuthor Commented:
Note: I will be AFK for the weekend and will be checking on Monday for responses.
bhlangAuthor Commented:
OK. Your answer looks pretty good.
Some additional info:
The second drop-down is pre-populated with Product Sources. Can I select an item from that list with Javascript? Basically I want the user to select a product from the first list. This will select the standard Product Source and fill in the Product Number. Then if the user wants to select a different Product Source, they can.
bhlangAuthor Commented:
Also, how can I extend this to a multi-line invoice? The example posted works fine for one line. I need to extend to multiple lines.
I'm using hierarchical field names:
bhlangAuthor Commented:
Question was partially answered, and then no followup came for my follow up questions. The solution as provided did what I needed, but was incomplete.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.