Skip to main content
Participating Frequently
Jun 24, 2022 2:43 AM UTC
Question

Scripting dynamic combo boxes

Hello. I am trying to script 2 to 3 comboboxes. The first box would have multiple choices of types of pipe threads I make and each successive combo box would be narrowed by the previous choice.  

I feel there is some type of security issue or I am not running the code correctly. The code works as it should and then it does not. Just looking for some pointers. I am using Acrobat dc and have javascript enabled. Here is an example of what I am struggling with. I will be adding more switch statements with if conditions.  The reason I think my setup is the problem is because I have followed tutorials to the T and am not getting the output I expect or any at times, no errors just nothing. Should I not be checking for functionality with "preview"?  I believe I am working with an interactive pdf.

Any advice is appreciated Thanks

var threadType = this.getField("Dropdownbox1");
var threadDiameter = this.getField("Dropdownbox2");
var weightPerFoot = this.getField(Dropdownbox3);

if (event.willCommit)
    switch (event.value) {
        case "Blue":
            threadDiameter.setItems["2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"];
            break;
    }

 

This topic is closed to new replies. Start a new post to keep the conversation going.

21 replies

Participating Frequently
Jul 1, 2022 5:17 PM UTC

Thank you for all of the information, it has been very helpful. I have been enjoying working through my project and trying to make things functional. I relize that I am just learning this workflow and Im sure my coding is not the best.  Currently I am trying to populate a textBox with a calculated var derived from the Sum of two vars, I can console the the calculated var and I get my desired number but if  I console same var with .value it is undefined. I cannot .setItems to the textbox. Getting "invalid argument type". I can access the information I need via console but cannot implement/assign it to "control"/textbox within my code. Any suggestions are appreciated. I have attatched a screenshot with failing path highlighted. Starting where I am setting up a var =  .getField  -to-   var.setItems of control/textBox that I am trying to populate(which fails to "invalid argument type"). highlighted.

Thanks very much for any advice

Translate
Participating Frequently
Jul 1, 2022 5:31 PM UTC

Here is a the .pdf

Translate
bebarth
Community Expert
Community Expert
Jul 1, 2022 9:41 PM UTC

Hi,

A real code would be better than a screenshot...

@+

Translate
bebarth
Community Expert
Community Expert
Jun 24, 2022 11:53 AM UTC

Hi,

If I understood correctly, place this script in custom keystroke script of the Dropdownbox1 field:

var threadType = this.getField("Dropdownbox1");
var threadDiameter = this.getField("Dropdownbox2");
var weightPerFoot = this.getField("Dropdownbox3");
if (!event.willCommit) {
	threadDiameter.clearItems();
	switch (event.changeEx) {
		case "Blue":
			threadDiameter.setItems(["2.375", "2.875", "3.50", "4.50", "5.50", "7.00", "9.625"]);
			break;
		case "Red":
			threadDiameter.setItems(["1.375", "2.875", "3.50", "4.50", "5.50", "6.00", "7.625"]);
			break;
		// etc.
	}
	if (event.changeEx!=0) threadDiameter.insertItemAt("- Thread Diameter -", 0);
}

@+

Translate
Participating Frequently
Jun 24, 2022 12:51 PM UTC

Thank you. I am not having succsess with the code installed in "custom key script" DropdownBox1.

I noticed the file and some parent files were read only and they keep being saved in some onedrive folder. It seems like this may be part of my problem. I am now right clicking and changing file to "Always Keep on This Device".  Still not getting desired outcome. Thanks

Translate
bebarth
Community Expert
Community Expert
Jun 24, 2022 2:32 PM UTC

If you want it to work, you have to check that the field names and data of the files and the script match.

@+

Translate
try67
Community Expert
Community Expert
Jun 24, 2022 7:58 AM UTC

Where did you place the code? Can you share the file?

Translate
Participating Frequently
Jun 24, 2022 12:46 PM UTC

I have been putting it my code in the Dropdownbox1 Custom Key Script. Here is a rough idea of where I am. I havent moved forward because I havent been able to get consistant results yet with where I am.

 

It seems like One Drive keeps messing up all my file locations and permissions.  I wonder if that is affecting the functionality.

Translate
Legend
Jun 24, 2022 12:57 PM UTC

When you're testing a PDF form it's vital to open the JavaScript console. It won't pop up a box telling you there is an error in your script, it all goes to the console. When I choose Blue the console says "TypeError: threadDiameter is null -- 5:AcroForm:Dropdown1:Keystroke"

Translate
Bernd Alheit
Community Expert
Community Expert
Jun 24, 2022 5:26 AM UTC

What output does you expect?

Translate
Participating Frequently
Jun 24, 2022 12:39 PM UTC

I would like each successive combo box  (Dropdown2), (Dropdown3) filter my choices based on previous selection. I was hoping I could script (Dropdown1) via custom keyscript with one switch/case statement to control/populate  (Dropdown2) and (Dropdown3). I will add unfinished rough pdf

Translate