haley
haley

Reputation: 75

Set Qualtrics Embedded Data with Javascript for loop

I am working on a survey in qualtrics, and need to be able to set many embedded data fields with different values, depending on a random selection of the experimental condition.

In the Survey Flow, I've entered all the names of the desired embedded data values.

I want this code to execute at the start of the survey, and to go through and set the values of those Embedded data variables.

The reason I use a for loop is because the variables are named things like: set1a set1b set1c

var condition  = Math.floor((Math.random() * 3) + 1)
var conditions =[ [8,8,8,5,5,5,5,2,2,2], [8,8,8,7,7,7,7,2,2,2], [10,10,10,5,5,5,5,2,2,2], [10,10,10,7,7,7,7,2,2,2]]
var values = condition[conditions]
var letters ="abcdefghij"


Qualtrics.SurveyEngine.addOnload(function(values)
{
    for (var i = 0; i < values.length; i++ ) {
        var dataname = "set1" + letters.charAt(i) 
        Qualtrics.SurveyEngine.setEmbeddedData(dataname,values[i]);
     }
 });

However, this code doesn't work. When I try to call the variables using piped text later on in the survey (after a page break) they do not appear to have stored.

Upvotes: 3

Views: 845

Answers (1)

Mike Hudecheck
Mike Hudecheck

Reputation: 58

You have two issues.

First, you can only set Javascript in Qualtrics if it's inside of the .addOnload function, e.g.:

Qualtrics.SurveyEngine.addOnload(function(values)
{
// Your code
});

Second, your method for assigning - values - throws an error message.

Qualtrics doesn't have an issue saving embedded data in a for loop, so if you fix these two points, you should be good to go.

Upvotes: 1

Related Questions