Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Aug 18, 2024 Aug 18, 2024

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!

TOPICS
JavaScript
845
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2024 Aug 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2024 Aug 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

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2024 Aug 18, 2024

What are the names of the dropdowns?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2024 Aug 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!

Charlie303735890y2h_0-1723991271752.pngexpand image

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2024 Aug 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2024 Aug 18, 2024

So if i put that code in the drop down box code it will work? sorry for the silly question i have no idea what im doing with java script

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2024 Aug 18, 2024

There's more to it than that, but that's how you can identify the corresponding fields with one script.  If you read through the articles and watch the videos in my previous post, all is explained.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 20, 2024 Aug 20, 2024
LATEST

The script im using works based on the names of the text boxes. The naming convention of the dropdown boxes in this script does not matter. Only the three text boxes each selection in the drop down populates. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines