Have an idea?

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

hidden reference variable not working with javascript

I have a section of questions asking about drug use: if they've ever used drugs, if (some) are prescribed, and if those that are prescribed are used as directed. I wanted to use a hidden variable to create a reference with 1 = ever used and 0 = never used drugs. I wanted to use these for skips later in the survey.

I got it so that this shorter code works:

<input name="ref4_drugever" id="ref4_drugever" type="hidden"></input>
<script>
var ref = 0;

if ([% g7i1p_1 %] == 1 && ([% g7iip1.1 %] == 0 || ([% g7iip1.1 %] == 1 && [% g7iip2.1 %] == 1 )))
{
    ref = 1;
}
else if([% g7i1p_2 %] == 1)
{
    ref = 1;
}
else
{
    ref = 0;
}


$("#ref4_drugever").val(ref);
</script>


But when I try to add all of the variables in, it does not work:


<input name="ref4_drugever" id="ref4_drugever" type="hidden"></input>
<script>
var ref = 0;

if ([% g7i1p_1 %] == 1 && ([% g7iip1.1 %] == 0 || ([% g7iip1.1 %] == 1 && [% g7iip2.1 %] == 1 )))
{ref = 1;}
else if([% g7i1p_2 %] == 1)
{ref = 1;}
else if([% g7i1p_3 %] == 1)
{ref = 1;}
else if([% g7i1p_4 %] == 1)
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.16 %] == 0 || ([% g7iip1.16 %] == 1 && [% g7iip2.16 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.17 %] == 0 || ([% g7iip1.17 %] == 1 && [% g7iip2.17 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.18 %] == 0 || ([% g7iip1.18 %] == 1 && [% g7iip2.18 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.19 %] == 0 || ([% g7iip1.19 %] == 1 && [% g7iip2.19 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.20 %] == 0 || ([% g7iip1.20 %] == 1 && [% g7iip2.20 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.21 %] == 0 || ([% g7iip1.21 %] == 1 && [% g7iip2.21 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.22 %] == 0 || ([% g7iip1.22 %] == 1 && [% g7iip2.22 %] == 1 )))
{ref = 1;}
else if([% g7i1p_5 %] == 1 && ([% g7iip1.23 %] == 0 || ([% g7iip1.23 %] == 1 && [% g7iip2.23 %] == 1 )))
{ref = 1;}
else if([% g7i1p_6 %] == 1)
{ref = 1;}
else if([% g7i1p_7 %] == 1)
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.24 %] == 0 || ([% g7iip1.24 %] == 1 && [% g7iip2.24 %] == 1 )))
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.25 %] == 0 || ([% g7iip1.25 %] == 1 && [% g7iip2.25 %] == 1 )))
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.26 %] == 0 || ([% g7iip1.26 %] == 1 && [% g7iip2.26 %] == 1 )))
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.27 %] == 0 || ([% g7iip1.27 %] == 1 && [% g7iip2.27 %] == 1 )))
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.28 %] == 0 || ([% g7iip1.28 %] == 1 && [% g7iip2.28 %] == 1 )))
{ref = 1;}
else if([% g7i1p_8 %] == 1 && ([% g7iip1.29 %] == 0 || ([% g7iip1.29 %] == 1 && [% g7iip2.29 %] == 1 )))
{ref = 1;}
else if([% g7i1p_9 %] == 1 && ([% g7iip1.30 %] == 0 || ([% g7iip1.30 %] == 1 && [% g7iip2.30 %] == 1 )))
{ref = 1;}
else if([% g7i1p_9 %] == 1 && ([% g7iip1.31 %] == 0 || ([% g7iip1.31 %] == 1 && [% g7iip2.31 %] == 1 )))
{ref = 1;}
else if([% g7i1p_9 %] == 1 && ([% g7iip1.32 %] == 0 || ([% g7iip1.32 %] == 1 && [% g7iip2.32 %] == 1 )))
{ref = 1;}
else if([% g7i1p_9 %] == 1 && ([% g7iip1.33 %] == 0 || ([% g7iip1.33 %] == 1 && [% g7iip2.33 %] == 1 )))
{ref = 1;}
else if([% g7i1p_9 %] == 1 && ([% g7iip1.34 %] == 0 || ([% g7iip1.34 %] == 1 && [% g7iip2.34 %] == 1 )))
{ref = 1;}
else if([% g7i1p_10 %] == 1)
{ref = 1;}
else if([% g7i1p_11 %] == 1)
{ref = 1;}
else if([% g7i1p_12 %] == 1 && ([% g7iip1.12 %] == 0 || ([% g7iip1.12 %] == 1 && [% g7iip2.12 %] == 1 )))
{ref = 1;}
else if([% g7i1p_13 %] == 1 && ([% g7iip1.13 %] == 0 || ([% g7iip1.13 %] == 1 && [% g7iip2.13 %] == 1 )))
{ref = 1;}
else if([% g7i1p_14 %] == 1)
{ref = 0;}
else if([% g7i1p_15 %] == 1)
{ref = 0;}
else
{ref = 0;}

$("#ref4_drugever").val(ref);
</script>



Can anybody offer advice on what I'm doing wrong or how I might debug? Thanks
asked May 8, 2015 by ab3886 (310 points)

Your answer

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