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

Use a loop to check dropdown field answer, then add corresponding field to a summary box

Explorer ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

I have a form with 21 rows and corresponding field names that range from 1-21. I would like to create a summary box on the final page of the document that checks the dropdown field titled "Status" and if the status is set to "Secured," to add the text in "FundingOpp" to the list (with a comma to separate entries). I set up a loop with my best guess of the coding to do this, but I am getting a syntax error in the code so I can't even test it. So I need help a) determining where the syntax error is and b) if the code as written (plus the edit for the error) will actually do what I'm guessing it will.

var maxFields = 21;

for (var i=1; i<=maxFields; i++) {
	var AcceptedStatus = this.getField("Status"+i).valueAsString;

if (AcceptedStatus=="Submitted" || AcceptedStatus=="Denied" || AcceptedStatus=="Revisit at a Later Date") continue;

var s=this.getField("FundingOpp"+i).value + ", " +;}
event.value = s;

 

Error reads

"SyntaxError: syntax error

8: at line 9"

 

Thank you very much for any help you can provide!

TOPICS
JavaScript , PDF forms

Views

394

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 , Apr 29, 2021 Apr 29, 2021

The syntax error is there:

 var s=this.getField("FundingOpp"+i).value + ", " +; 

 

Votes

Translate

Translate
Community Expert , Apr 30, 2021 Apr 30, 2021

a) See above.

b) No, because you're overwriting the value of the "s" variable in each iteration of the loop. You need to define this variable before the loop and then add to its current value within in, like this:

 

var maxFields = 21;
var s = "";
for (var i=1; i<=maxFields; i++) {
	var AcceptedStatus = this.getField("Status"+i).valueAsString;
	if (AcceptedStatus=="Submitted" || AcceptedStatus=="Denied" || AcceptedStatus=="Revisit at a Later Date") continue;
	if (s!="") s+=", ";
	s+=this.getFiel
...

Votes

Translate

Translate
Community Expert ,
Apr 29, 2021 Apr 29, 2021

Copy link to clipboard

Copied

The syntax error is there:

 var s=this.getField("FundingOpp"+i).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
Community Expert ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

a) See above.

b) No, because you're overwriting the value of the "s" variable in each iteration of the loop. You need to define this variable before the loop and then add to its current value within in, like this:

 

var maxFields = 21;
var s = "";
for (var i=1; i<=maxFields; i++) {
	var AcceptedStatus = this.getField("Status"+i).valueAsString;
	if (AcceptedStatus=="Submitted" || AcceptedStatus=="Denied" || AcceptedStatus=="Revisit at a Later Date") continue;
	if (s!="") s+=", ";
	s+=this.getField("FundingOpp"+i).valueAsString;
}
event.value = s;

 

I also changed it a bit so you don't end up with a loose comma at the end of the list...

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 ,
Apr 30, 2021 Apr 30, 2021

Copy link to clipboard

Copied

LATEST

Brilliant, works beautifully! Also, adding the comma as long as the value is not empty before adding the additional fields is so smart, definitely squirreling that info away! And love the example of establishing variable S outside of the loop to avoid it being rewritten but instead added to. Elegant solution that I learned a lot from!

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