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

Dependent Dropdown JavaScript Syntax Error

Community Beginner ,
Sep 06, 2024 Sep 06, 2024

I pulled up instructions to write JavaScript to make a dependent dropdown but I keep getting a syntax error and I can't figure it out. Can someone help? I've never done this before so forgive me for missing the obvious. Thank you!

var f = this.getField(“Program”);
switch(event.value){
case “RSI - The Refrigeration School"
f.setItems([“AOS in Mechanical Maintenance Engineering”, “AOS in Mechanical Maintenance Engineering MTP”, “Electrical Applications”, “Electrical Applications MTP”, “Electro-Mechanical Technologies”, “Electro-Mechanical Technologies MTP”, “Refrigeration Technologies”, “Refrigeration Technologies MTP”, “Welding Specialist”, “Welding Specialist MTP”]);
break;

case “TWS Tulsa"
f.setItems([“AOS in Welding Technology”, “AOS in Welding Technolgoy MTP”, “Electrical Applications”, “Electrical Applications MTP”, “Electro-Mechanical Technologies”, “Electro-Mechanical Technologies MTP”, “Refrigeration Technologies”, “Refrigeration Technologies MTP”, “Professional Welder”, “Professional Welder MTP”]);
break;

default:
f.setItems([""]);
}

 

scriptsyntaxerror.PNGexpand image

TOPICS
JavaScript
932
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Sep 06, 2024 Sep 06, 2024

There are two types of errors in the code.

1) Bad quotes.  In JavaScript the plain ASCII quote must be used  " instead of 

2)"Case" statements end with a ":"   Check the Core JS reference for syntax specifics.  

Here's what it should be (except I didn't fix all your quotes, you'll need to do that):

 

var f = this.getField("Program");
switch(event.value){
  case "RSI - The Refrigeration School":
    f.setItems(<replace quotes>);
    break;

  case "TWS Tulsa":
    f.setItems(<replace quotes>);
    break;

  default:
    f.setItems([""]);
}

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

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 ,
Sep 06, 2024 Sep 06, 2024

If you are populating a text field you can't use setItems(), that is for dropdown fields, instead use like this:
f.value = "$18,700";

View solution in original post

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 ,
Sep 06, 2024 Sep 06, 2024

There are two types of errors in the code.

1) Bad quotes.  In JavaScript the plain ASCII quote must be used  " instead of 

2)"Case" statements end with a ":"   Check the Core JS reference for syntax specifics.  

Here's what it should be (except I didn't fix all your quotes, you'll need to do that):

 

var f = this.getField("Program");
switch(event.value){
  case "RSI - The Refrigeration School":
    f.setItems(<replace quotes>);
    break;

  case "TWS Tulsa":
    f.setItems(<replace quotes>);
    break;

  default:
    f.setItems([""]);
}

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Sep 06, 2024 Sep 06, 2024

@Thom Parker I would have never gotten the ASCII quotes. I really appreciate your help and explanation. Thank you!

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 ,
Sep 06, 2024 Sep 06, 2024

@Thom Parker 

Ok, I fixed that but it's still showing an error with this line: switch(event.value){

Any idea what I've done wrong? Should there be spaces somewhere? Thank you!

 

var f = this.getField(“Program”);
switch(event.value){

case “RSI - The Refrigeration School”:
f.setItems([“AOS in Mechanical Maintenance Engineering”, “AOS in Mechanical Maintenance Engineering MTP”, “Electrical Applications”, “Electrical Applications MTP”, “Electro-Mechanical Technologies”, “Electro-Mechanical Technologies MTP”, “Refrigeration Technologies”, “Refrigeration Technologies MTP”, “Welding Specialist”, “Welding Specialist MTP”]);
break;

case “TWS - Tulsa”:
f.setItems([“AOS in Welding Technology”, “AOS in Welding Technolgoy MTP”, “Electrical Applications”, “Electrical Applications MTP”, “Electro-Mechanical Technologies”, “Electro-Mechanical Technologies MTP”, “Refrigeration Technologies”, “Refrigeration Technologies MTP”, “Professional Welder”, “Professional Welder MTP”]);
break;

default:
f.setItems([“”]);
}

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 ,
Sep 06, 2024 Sep 06, 2024

From what I can see, the code still contains many non-ASCII quotes. In fact, it looks like all non-ASCII quotes. 

 

The ASCII qotes are straight up and down, no curlies. 

Non-ASCII quotes are introduced when the script is developed in, or copied using, a word processing app, such as word. Use a plain text or code editor to develop scripts. My favorite is NotePad++. It's free. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Sep 06, 2024 Sep 06, 2024

Ah, ok. I was using Alt+ the code in word so I guess that would cause the problem. Ok I will try it in something else. Thank you again for your patience and help!

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 ,
Sep 06, 2024 Sep 06, 2024

@Thom Parker I set up the code in notepad and it worked perfectly! Thank you again for your help!

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 ,
Sep 06, 2024 Sep 06, 2024

@Thom Parker I'm wondering if you know if it's possible to populate a numeric formatted text field from a dropdown selection? For example if I selected 'milk' in a 'groceries' dropdown list it would populate '$2.00' in a 'Cost' number formatted text field. I tried the script below and the 'Tuition' field doesn't populate. I also tried changing to a 'Tuition2' dropdown and entering the dollar amounts as a list in options but it keeps giving me formatting errors for the number no matter what change the options to. Thank you!

 

var f = this.getField("Tuition");
switch(event.value){

case "Professional Welder":
f.setItems(["$18,700"]);
break;

case "Professional Welder MTP":
f.setItems(["$16,830"]);
break;

case "Welding Specialist with Pipefitting":
f.setItems(["$22,650"]);
break;

case "Welding Specialist with Pipefitting MTP":
f.setItems(["$20,385"]);
break;

case "AOS in Welding Technology":
f.setItems(["$17,500"]);
break;

case "AOS in Welding Technology MTP":
f.setItems(["$15,750"]);
break;

case "Electrical Applications":
f.setItems(["$16,900"]);
break;

case "Electrical Applications MTP":
f.setItems(["$15,210"]);
break;

case "Electro-Mechanical Technologies":
f.setItems(["$19,700"]);
break;

case "Electro-Mechanical Technologies MTP":
f.setItems(["$17,730"]);
break;

case "Refrigeration Technologies":
f.setItems(["$16,900"]);
break;

case "Refrigeration Technologies MTP":
f.setItems(["$15,210"]);
break;

case "Electrical Lineworker":
f.setItems(["$13,750"]);
break;

case "Electrical Technologies":
f.setItems(["$16,900"]);
break;

case "Electrical Technologies MTP":
f.setItems(["$15,210"]);
break;

default:
f.setItems([""]);
}

 

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 ,
Sep 06, 2024 Sep 06, 2024

If you are populating a text field you can't use setItems(), that is for dropdown fields, instead use like this:
f.value = "$18,700";

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 ,
Sep 08, 2024 Sep 08, 2024

@Nesa Nurani thank you so much!

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 ,
Sep 07, 2024 Sep 07, 2024

Please take a looks at this article and the associated example PDF.

https://acrobatusers.com/tutorials/change_another_field/

 

The article uses a more sophisticated technique than you probably need, but the concept is the same for any technique. 

1) Use an event on the dropdown that is triggered by a selection.

2) Write code that uses the new selection data to fill other fields. 

 

A simple method is to use the Validate event on the dropdown with a script that looks something like this.
(which is similar to the code you posted above)

 

var f = this.getField("Cost");
switch(event.value){
  case "Professional Welder":
    f.value = 18700;
    break;
  case "Professional Welder MTP":
    f.value = 16830;
    break;
}

 

 

Note that the numbers are not formatted. They are plain numbers. The formatting is done by the cost field.

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Sep 08, 2024 Sep 08, 2024
LATEST

@Thom Parker thank you for the awesome article and once again the help! This is amazing and it totally worked for me. I really appreciate it!

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