Skip to main content
Known Participant
August 18, 2024
Question

Javascript code / variable for multiple drop downs instead of duplicating code

  • August 18, 2024
  • 3 replies
  • 1107 views

I have a document which has lots of drop downs. i have added code so that different drop down options auto populate weight, description in text boxes for various products.

 

The issue is when using the 'create multiple copies' as every drop down wants to be the same. You have to manually edit each code, give different names to different text fields to populate different text boxes. I am wondering if anyone could help me out in a better way of doing this. If i need to change a weight or product about 40 drop downs will also have to change as opposed to just changing one bit of code on one drop down. Any help is much appreicated. 

 

thank you!

This topic has been closed for replies.

3 replies

Bernd Alheit
Community Expert
Community Expert
August 18, 2024

What are the names of the dropdowns?

Known Participant
August 18, 2024

Here is a screenshoto of some of the drop downs. all have similar names with slight variations. i then input each naming difference in the code int  eaoh named dropdown. If that makes sense. Thank you for the reply!

 

PDF Automation Station
Community Expert
Community Expert
August 18, 2024

Assuming the fields you want to populate are in the same row with the same suffixes (because you created multiple copies at the same time) the script to define the corresponding field names would be:

var num=event.target.name.replace("Dropdown.1.4.5.1.","");

this.getField("this.getField("QTY.1.0."+num)//etc.

 

PDF Automation Station
Community Expert
Community Expert
August 18, 2024

You can separate the export values in the dropdown with a separator like this:

A--B--C--D

then split the export value for the result.  You can either create a custom calculation script in each of the text fields that pulls the value of the dropdown like this:

1) event.value=this.getField("Dropdown").value.split("--")[0];

2) event.value=this.getField("Dropdown").value.split("--")[1];//etc.

Or, you can write a custom keystroke script that splits the changeEx value of the dropdown to the text fields like this:

if(!event.willCommit)

{

this.getField("QTY.1.0.0").value = event.changeEx.split("--")[0];

this.getField("Description.1.0.0").value = event.changeEx.split("--")[0];//etc.

}

Enter your scripts prior to creating multiple copies, then aniticipate what the field names will be after creating multiple copes, extract the suffix of the field names into a variable, and add the variable back into the field names in the script.  Using this method allows you to write one script, once, that will be in all the copies and will work in each copy.  Here's some guidance for all of this:

https://pdfautomationstation.substack.com/p/calculation-vs-validation-scripts-eb5

https://www.youtube.com/watch?v=vsUKJtHtoMI

https://pdfautomationstation.substack.com/p/anticipating-field-names-in-custom

 

 

Known Participant
August 18, 2024

if (event.value=="") {
this.getField("QTY.1.0.0").value = "";
this.etField("Description.1.0.0").value= "";
this.getField("WEIGHT1.0.0").value = "";

} else
this.getField("QTY.1.0.0").value = "";
this.getField("Description.1.0.0").value = "";
this.getField("WEIGHT1.0.0").value = "";
}

This is the code i am using in the drop down. I have to create multiple copies of each drop down. then edit the code between the "" for a long list of items for each drop down. So i am wondering if there is an easier way to do this.