Have an idea?

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

Initializing fields/variables via SETVALUE in Perl

Hi all,

is it possible to initialize fields via unverified perl with SETVALUE?
I have a "page" where I want to set different questionaire variables (different screens & types) but when I visit the actual pages the input items don't reflect the values I've set (only tried it in local development mode)

Thanks & best regards
Jens
asked Jul 23 by anonymous
Is there a reason this would need to be done using Sawtooth Script?  Otherwise, we could just address this with JavaScript.

1 Answer

0 votes
SETVALUE allows you to fix the value of a variable. At the same time, it also allows you to change the values when the question appears without showing the already set value.

It seems you want to fix a value of a variable and show respondent the already set value, he can change the values if he wants to. Right?

In this case, you can set the value using java-script (document ready) and the script needs to be written on each specific page. The value will appear on the survey and the respondent can also change that.

Hope this is the information you were looking for.

Regards,
Rahul Mittal
answered Jul 23 by Rahul Mittal Bronze (4,980 points)
Yeah exactly. Is it only possible to "initialize" (=set) variables (scattered over multiple "screens") via Javascript on the screen I want to display it's "contents" radio/input type text/... ?
Yes, you can set all variables and show them when the questions appears. But you need to apply javascript on page where the variable is present. It will show the required value selected and respondent can change if he wants.

Suppose you want to set value of QX = 3 and QX is a single select question. You can use below and copy in footer of QX.

<script>
$(document).ready(function(){
SSI_SetSelect("QX_3",true);
});
</script>
One potential downside is that this will reset the question if the respondent has already answered it (i.e., answered the question, submitted the page, and then went back).  If we surround the setter with a conditional, we can make it only run if the question is unanswered.  Continuing the radio-type select question example:

<script>
$(document).ready(function(){
    if (!SSI_GetValue('[% QuestionName() %]_3')) {
        SSI_SetSelect('[% QuestionName() %]_3', true);
    }
})
</script>
True, Thank you Zachary, for making it appropriate.
...