Have an idea?

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

How do I set a variable for a checkbox type question

I have a mark all that apply question with 5 options.  The variable name is var1 and type checkbox.  I would like to set the fifth option to 1 if a pervious question on an early page was answered yes.  I’m new to JavaScript and trying to find the correct code for setting the variable to 1.

Here is code I’m trying to use.
<script type="text/javascript">
<!--

var n=([%Q2_var1%]);  //previous answer
if (n == 1)
 { 
   document.getElementById("Q6_var1_5").value = 1;
 }

//-->
</script>
asked Dec 22, 2011 by Greg Bronze (910 points)
edited Dec 22, 2011 by Gary Baker

1 Answer

+1 vote
<script type="text/javascript">
<!--
document.onreadystatechange = afterLoad;
function afterLoad()
{
var n=([%Q2_var1%]);  //previous answer
if (n == 1)
 {
   document.getElementById("Q6_var1_5").checked = true;
 }
}
//-->
</script>
should work, let me know if it does not
answered Dec 22, 2011 by Bahadir Ozkurt Gold (16,930 points)
edited Dec 22, 2011 by Bahadir Ozkurt
It didn't work for me.  I'm placing the code in the HTML <head> Tag under the advanced tab.  I even tried to place it under the Custom JavaScript Verification tab.  I run the page, answer the question but when I look at the data, the variable is set to zero. Just to test the code some more, I placed just the "document..." line in the HTML <head> tag thinking it would check the box but it did not.
That worked.  Not your bad, my bad.  I don't understand enough how HTML and JavaScript works.  Back to w3school for me.  Thanks a bunch!
you're welcome
How did you finally get it to work? I tried with
document.getElementById("Q6_var1_5").checked = true;

as well as

document.getElementById("Q6_var1_5_graphical").checked = true;

but none worked. What am I missing
Nevermind, I figured it out. I just replaced document.onreadystatechange with window.onload
yeah this is from my earlier JS days :)
...