Have an idea?

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

Split sample set of questions on single page

I have a set of questions Q13 through Q18, with Q15a and Q15b falling in the middle. I have this page randomizing all questions on the page. I need Q15a to show to half the respondents and Q15b to the other.  This is what I have been trying:

<style>
    #Q15a_div {display: none;}
    #Q15b_div {display: none;}
</style>

<script type="text/javascript">
$(document).ready(function(){
    if ([% RespNum() mod 2 = 0 %]) {
            document.getElementById('Q15a_div').style.display = "block";
        }
    if ([% RespNum() mod 2 = 1 %]) {
            document.getElementById('Q15b_div').style.display = "block";
        }
})
</script>
asked Oct 4 by Ron H.

1 Answer

0 votes
You've got a great start.  This would work if not for a quirk in how Sawtooth Script prints out false values.  One way to work around this particular quirk is to force JavaScript to convert the Sawtooth Script output into a number.

<style>
#Q15a_div,
#Q15b_div {
    display: none;
}
</style>

<script>
$(document).ready(function(){
    if (Number('[% RespNum() Mod 2 = 0 %]')) {
        document.getElementById('Q15a_div').style.display = "block";
    }
    if (Number('[% RespNum() Mod 2 = 1 %]')) {
        document.getElementById('Q15b_div').style.display = "block";
    }
})
</script>
answered Oct 4 by Zachary Platinum Sawtooth Software, Inc. (63,475 points)
Or, if you prefer, you could do the modular operation in JavaScript instead of Sawtooth Script to avoid the quirk:

<style>
#Q15a_div,
#Q15b_div {
    display: none;
}
</style>
 
<script>
$(document).ready(function(){
    if ([% RespNum() %] % 2 == 0) {
        document.getElementById('Q15a_div').style.display = "block";
    }
    if ([% RespNum() %] % 2 == 1) {
        document.getElementById('Q15b_div').style.display = "block";
    }
})
</script>
Ah, I see. Thanks for working that out, this now works great!
Odd thing happening on some systems it seems. On my browser it works fine, but am getting some screen shots from testers showing that question text is missing on the displayed question - the response options are there, but the header text is not showing up. Any ideas?
That doesn't sound like it would be caused by my code.  If you remove my code, does the missing text behavior change?
Sorry, found some remnant code in the split B question header that was causing this. Thanks again!
...