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

Help debugging JavaScript

Explorer ,
Nov 06, 2023 Nov 06, 2023

Copy link to clipboard

Copied

Admissions up front, I don't know coding. This has been piece-mealed from my own reasearch with a bit of assistance from ChatGPT.

I have a form with two dropdown fields; "News Outlet" and "Author". I want the "Author" drop-down to only populate with a select list of authors based on the chosen News Outlet.

This is the code that I have written in the News Outlet validate tab:

var newsOutletField = this.getField("News Outlet");
var authorField = this.getField("Author");

 

// Define a mapping of News Outlets to their corresponding authors
var authorsByOutlet = {
"World News Today": ["Luarie Lee", "Pete Downer", "Jake Fuller"],
"Global News Network": ["Barbara Marcus", "William Donovan", "Duke Cannon"],
"Associated Press": ["Steve Plank", "Susie Grove", "Murray Slaughter"],
"News Online": ["Martin Hall", "Brandy Cook", "Mary Richards"],
"Europe News Now": ["Mike Anderson", "Adam Carrington", "Rita Hanson"],
"Reuters": ["Robyn Cross", "Pam Turner", "Jake Frampton"]
};

 

// Function to update the authors based on the selected News Outlet
function updateAuthors() {
var selectedOutlet = newsOutletField.valueAsString;
authorField.clearItems();
var authors = authorsByOutlet[selectedOutlet];
if (authors) {
authorField.setItems(authors);
}
}

// Add a "Validate" action to the "News Outlet" dropdown field
newsOutletField.setAction("Validate", "updateAuthors();");

// Initially, populate the authors based on the default selection
updateAuthors();

When it runs, (preview form) for some reason it erases all of the code and the only thing left in the script box is:
updateAuthors();

During that first preview run, the Author drop-down populates with a select list of authors, and the list updates, but they're the wrong associations. If I exit preview and then preview form again, the list remains with the last selection of authors and doesn't update anymore.

 

I'm not sure what's happening or why it's clearing all of the code. The Debug console hasn't been any help either, but that could just be my ignorance. However it doesn't report any errors in the console. 

Any help and/or insights would be appreciated. Document in question attached.

TOPICS
How to , JavaScript , PDF forms

Views

152

Translate

Translate

Report

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

correct answers 2 Correct answers

Community Expert , Nov 06, 2023 Nov 06, 2023

Use this as 'Validate' script of "News outlet" field and also in same field under 'Options' tab check 'Commit selected value immediately':

var authorField = this.getField("Author");
switch(event.value){
case "World News Today":
authorField.setItems(["Luarie Lee", "Pete Downer", "Jake Fuller"]);
break;
case "Global News Network":
authorField.setItems(["Barbara Marcus", "William Donovan", "Duke Cannon"]);
break;
case "Associated Press":
authorField.setItems(["Steve Plank", "Susie Grove", "Murray S
...

Votes

Translate

Translate
Community Expert , Nov 06, 2023 Nov 06, 2023

It's a two part script, the last section of the script uses setAction()  to automatically set the function into 'Validate':

// Add a "Validate" action to the "News Outlet" dropdown field
newsOutletField.setAction("Validate", "updateAuthors();");

// Initially, populate the authors based on the default selection
updateAuthors();

This is why your script was deleted as it is meant to be used as 'Document level' script which would then automatically set updateAuthors(); into 'Validate' script.

Even if

...

Votes

Translate

Translate
Community Expert ,
Nov 06, 2023 Nov 06, 2023

Copy link to clipboard

Copied

Use this as 'Validate' script of "News outlet" field and also in same field under 'Options' tab check 'Commit selected value immediately':

var authorField = this.getField("Author");
switch(event.value){
case "World News Today":
authorField.setItems(["Luarie Lee", "Pete Downer", "Jake Fuller"]);
break;
case "Global News Network":
authorField.setItems(["Barbara Marcus", "William Donovan", "Duke Cannon"]);
break;
case "Associated Press":
authorField.setItems(["Steve Plank", "Susie Grove", "Murray Slaughter"]);
break;
case "News Online":
authorField.setItems(["Martin Hall", "Brandy Cook", "Mary Richards"]);
break;
case "Europe News Now":
authorField.setItems(["Mike Anderson", "Adam Carrington", "Rita Hanson"]);
break;
case "Reuters":
authorField.setItems(["Robyn Cross", "Pam Turner", "Jake Frampton"]);
break;
default:
authorField.clearItems();}

Votes

Translate

Translate

Report

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
Explorer ,
Nov 06, 2023 Nov 06, 2023

Copy link to clipboard

Copied

That's great! And seemingly so much simpler than the other code.

Are you able to identify what was wrong with my original code and why it was breaking? Hoping to learn something from this. 🙂

Thank you very much for such a swift solution!

Votes

Translate

Translate

Report

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 ,
Nov 06, 2023 Nov 06, 2023

Copy link to clipboard

Copied

LATEST

It's a two part script, the last section of the script uses setAction()  to automatically set the function into 'Validate':

// Add a "Validate" action to the "News Outlet" dropdown field
newsOutletField.setAction("Validate", "updateAuthors();");

// Initially, populate the authors based on the default selection
updateAuthors();

This is why your script was deleted as it is meant to be used as 'Document level' script which would then automatically set updateAuthors(); into 'Validate' script.

Even if you did it correctly, you would have a delay, to make your script work properly you need to delete first line and change:

var selectedOutlet = newsOutletField.valueAsString;

to:

var selectedOutlet = event.value;

Votes

Translate

Translate

Report

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