Have an idea?

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

How to set merged field value with javascript

How do I set the value of a merged field with javascript?  There is no SSI_SetValue function in the js library.  I know I can do it with perl or an SSI system function, but I want to know how to do it in js.

I tried using the SSI function within my js (see below), but it appears that all SSI functions get evaluated regardless of the js logic.  For example:
if(Age < 18)
[% setvalue(Cust,"first value") %];
[% setvalue(Cust,"second value") %];

In this example, Cust is always set to "second value". (Yes, I debugged with Firebug and confirmed that Age was < 18.  The js logic is fine, but the SSI function setvalue is always evaluated (both of them).

Thanks in advance.
asked Oct 11, 2015 by cseitz (290 points)
Did you ever get an answer to this question? I am running in to the same problem

1 Answer

0 votes
I extracted this JavaScript variable information from the Sawtooth Software help that describes variable defining ...

JavaScript Variables

Variables in JavaScript can be declared with the "var" statement. For example:

var NumDaysInWeek = 7;

var MonthName = "March";

After this code is executed NumDaysInWeek will contain 7 and MonthName will contain "March".  Notice that quotes are used when assigning text to a variable.

To retrieve the value of a question on the current page you can use SSI Web's built-in SSI_GetValue(QuestionName) JavaScript function.  Given any question name (or variable name) this function will return the value entered for that question on the current page. For example:

var Q1Value = SSI_GetValue("Q1");

var BlueChosen = SSI_GetValue("FavColors_1");

In the above example, the function SSI_GetValue retrieves the value for Q1 as entered on the page.  It is then assigned to the Q1Value variable.  The next line retrieves the value for the first check box in a Select question.  BlueChosen is assigned a 1 if it is checked and a 0 otherwise.

To retrieve the response to a question from a prior page and use that value in your JavaScript, you should use SSI Script in conjunction with JavaScript.  For example, if NumWeeks was asked on a prior page of the survey and you wanted to use its value to do a simple calculation and display the result on the current screen, the following JavaScript could be used:

var NumDaysInWeek = 7;

var Weeks = [%NumWeeks%];

document.write(NumDaysInWeek * Weeks);

Before the web page comes down to the browser the SSI Script [%NumWeeks%] is changed to the value that was entered on a prior page by the respondent. Lets assume that value was 4. The JavaScript loaded by the browser is:

var Weeks = 4;

To load text from a prior question, use:

var Name = "[%LastName%]";

which when it arrives at the browser would be:

var Name = "Smith";

Arrays, which are variables that can hold a collection of items, are also available:

var ItemList = [1,2,3,4,5,6,7];


In the example above, an array of 7 items is declared.  The first item in the array (with value 1) is written to the screen.  Individual array items can be accessed with 0-based index numbers between [ and ]. So [0] returns the 1st item, [1] the 2nd, and so on.
answered Oct 11, 2015 by Paul Moon Platinum (59,425 points)
Thanks, but you didn't answer my question.