Have an idea?

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

Autosubmit question and set variable value by clicking a button

I have a button on my page.  When it is clicked, I want the variable to be set to the value of 9 and the user is automatically moved to the next page.  Interestingly, when I submit the code below and comment out one of the two lines, it works.  So I can automatically move to the next page, but the variable does not get a 9.  OR I can use the SetValue but the page won't submit.  I'm thinking that I'm mixing up programming languages.

<script>
function Skip() 
{
 
[% SetValue(mar1_r1,9) %];
setTimeout('SSI_SubmitMe()', 1000); 

}
</script>
asked Aug 18, 2014 by RhondaDick (180 points)
retagged Aug 18, 2014 by Walter Williams

2 Answers

0 votes
As is your SetValue will run server side before the page loads, so need to set it with JavaScript.

try.. (assuming mar1 is radio grid? adjust if not)

<script>
function Skip()
{
$("#mar1_r1_9_graphical").click();
setTimeout('SSI_SubmitMe()', 1000);

}
</script>
answered Aug 18, 2014 by 192
Mar1_r1 is a radio button.

I just tried and it didn't work.  I have it in the HTML header.  The page was automatically submitted, but it didn't save the mar1_r1 variable.

I block copied the code.

What am I missing.  And thanks for your help!
Try changing "#mar1_r1_9_graphical" to "#Mar1_r1_9_graphical", the capital M seemed to make the difference for me. (this also assumes that you are using the graphical radio buttons and you have at least 9 columns in the grid, just to be sure)
Just a heads up, that I'm definitely not very fluent in JavaScript.  And on top of it, I've confused things.

First, mar1_r1 is a survey variable radio button.  There are 7 responses for that variable.

The "button" I want to set 9 as a value for is just an input-type button (see code below).

input type="button" name="mar1_r1skip" value="Skip Question" onClick="Skip()">


Sorry for the confusion!
Ah, ya don't think you can set a select to a value not in the response list. Might want to make a free format variable as a on that same page as a hidden type variable input and set the value of that in your Skip function instead. Can consult the free format and jQuery Tips sections of the documentation for examples.

If you meant that mar1 is a free format radio variable I guess you could make it with 9 responses and hide the last two with CSS.

<style type="text/css">
.hideme {display:none}
</style>

<script type="text/javascript">
function Skip() {
    SSI_SetSelect("mar1_r1_9", true);
    setTimeout('SSI_SubmitMe()', 1000);
}

</script>

<button type="button" onclick="Skip()">Skip and set mar1_r1 to 9</button>

<table>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 1)%]
        </td>
        <td>
            ResponseOption_1
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 2)%]
        </td>
        <td>
            ResponseOption_2
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 3)%]
        </td>
        <td>
            ResponseOption_3
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 4)%]
        </td>
        <td>
            ResponseOption_4
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 5)%]
        </td>
        <td>
            ResponseOption_5
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 6)%]
        </td>
        <td>
            ResponseOption_6
        </td>
    </tr>
    <tr class="clickable">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 7)%]
        </td>
        <td>
            ResponseOption_7
        </td>
    </tr>
    <tr class="clickable hideme">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 8)%]
        </td>
        <td>
            ResponseOption_8
        </td>
    </tr>
    <tr class="clickable hideme">
        <td class="input_cell">
            [%RadioSelect(mar1_r1, 9)%]
        </td>
        <td>
            ResponseOption_9
        </td>
    </tr>
   
</table>
Yes, mar1 is a free format question.  I'll give this a try.  Thanks!
Np, goodluck. The "_r1" is the naming scheme they use for rows in a grid question so threw me off a bit.
0 votes
Hi Rhonda,

Here is the code that works for me.

<script type="text/javascript">

function Skip()
{
    
    // SetValue for ID Q2skip_nap to 98 then move to the next page
    document.getElementById("Q2skip_nap").value = "98";
    SSI_SubmitMe();
}

</script>

answered Dec 22, 2014 by Greg Bronze (890 points)
...