Skip to main content
Inspiring
April 30, 2021
Répondu

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

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!

Ce sujet a été fermé aux réponses.
Meilleure réponse par try67

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...

2 commentaires

try67
Community Expert
try67Community ExpertRéponse
Community Expert
April 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.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...

Inspiring
April 30, 2021

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!

Bernd Alheit
Community Expert
Community Expert
April 30, 2021

The syntax error is there:

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