Skip to main content
Participating Frequently
June 24, 2022
Question

Scripting dynamic combo boxes

  • June 24, 2022
  • 4 replies
  • 5641 views

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 has been closed for replies.

4 replies

Participating Frequently
July 1, 2022

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

Participating Frequently
July 1, 2022

Here is a the .pdf

bebarth
Community Expert
July 1, 2022

Hi,

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

@+

bebarth
Community Expert
June 24, 2022

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);
}

@+

Participating Frequently
June 24, 2022

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

bebarth
Community Expert
June 24, 2022

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

@+

try67
Community Expert
June 24, 2022

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

Participating Frequently
June 24, 2022

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.

Brainiac
June 24, 2022

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"

Bernd Alheit
Community Expert
June 24, 2022

What output does you expect?

Participating Frequently
June 24, 2022

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