Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

JavaScript fix when pre-filling check boxes from a previous question

I have 2 multiple response questions - X1 and X2. Both use the same list which consists of 10 options.

If I select options 1-5-10 at X1, X2 should display all 10 options but have the check boxes ticked for 1-5-10. I can do this with a series of if statements, but had trouble converting it to a for loop format.

I know the line "  if('[%X1%]_'+i==1)" is my problem.
<script>
$(document).ready(function(){
 for(var i=1; i<=10; i++)
  {
  if('[%X1%]_'+i==1)
   {
     SSI_SetSelect('[%QuestionName()%]_'+i, true);
   }
  }
})
</script>

Appreciate any guidance. Thanks.
asked Aug 16, 2022 by Paul Moon Platinum (101,255 points)
You need to read in all the values from the database before going into the loop.  The simplest way would be something like this:

var x1Resps = [[% X1_1 %], [% X1_2 %], [% X1_3 %], ...];


If you don't want to write out a bunch of calls, though, this will do the same thing:

var x1Resps = [% Begin Unverified Perl
my @resps = ();
for (my $i = 1; $i <= 10; $i++) {
    push(@resps, GETVALUE('X1_' . $i));
}
return '[' . join(',', @resps) . ']';
End Unverified %]


Either way, "x1Resps" is now an array of all the X1 responses and you can pull out whichever one you want in your loop.
Thanks Zachary for pointing me in the right direction. I tried both methods which worked. Once you had me capturing X1 within an array, I was able to call upon that array within my for loop. Much appreciated as always buddy.

My script now looks like this ...
<script>
$(document).ready(function(){

 var x1Resps=[[%X1_1%], [%X1_2%], [%X1_3%], [%X1_4%], [%X1_5%], [%X1_6%], [%X1_7%], [%X1_8%], [%X1_9%], [%X1_10%],];

 for(var i=1; i<=10; i++)
  {
  if(x1Resps[i-1]==1)
   {
     SSI_SetSelect('[%QuestionName()%]_'+i, true);
   }
  }
})
</script>
Nice final solution.  I had been watching for the response here as I couldn't wrap my head around it.  Makes sense now.
Yeh Jay, I knew what I initially did would not work. The piping in of all 10 variables from X1 wasn't correct when I tried to use a for loop format. I had it working just fine with a series of if statements.

But Zachary the wizard guided me in to storing the 10 variables in to an array and operating the loop off that. Then I was up and running ...

I tested both of Zachary's array solutions out which worked fine.
Yeah, I was able to go through the script and make sense of it, but I have not personally done anything with arrays.  It took me a minute and then I understood the '-1' as they always start with 0 as I recall.
Yes correct. The arrays start at index "0", hence the reason I popped in the -1 to align with the for loop counter.

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...