Javascript for Qualtrics: performing mathematical operations on quantitative responses

In Qualtrics, I needed to take a quantitative survey response, do some mathematical operations to the response, and then (1) display the output to the survey respondent and (2) write the output to the Qualtrics dataset. It turns out this is a little tricky, but as described in this post, Javascript can do this task nicely.

There might be a way to do this Qualtrics “Scoring,” but as of this writing in 2015, that thing is a hot mess. Its user interface is diabolically non-intuitive. After a particularly fierce battle with “Scoring,” I vowed never to use it again.

The good news is that Javascript can do the task nicely. It’s a little tricky to get everything working, so I’ll document a basic version of it here.

First, it’s necessary to initialize the Embedded Data variable in the Qualtrics Survey Flow. I’ve chosen to name the variable “Output.” (Screenshot)

Second, a slider can be used to take the quantitative input from the survey participant. (Screenshot)

Third, a simple display question and a timer with a one-second auto-advance work together to give the Javascript code a place to reside. (Screenshot) When the Javascript is put into the display question, the code loads, runs, and finshes after the slider question and before the output display.

The Javascript in the display question performs the mathematical operation. I will comment more on the code below. (Screenshot)

Fourth, a simple display question can be used to show (1) the slider value previously chosen by the respondent and (2) the output of the mathematical operation. Note that the second line of the display is simply piping the Embedded Data variable “Output” to the screen. The “Output” variable also shows up in the data when it’s exported from Qualtrics. (Screenshot)

When the survey is run, the input (Screenshot) gives the desired output (Screenshot).

The Javascript code is shown more clearly below. The mathematical operation is a simple one that demonstrates the use of arithmetic and a Javascript math function.

Qualtrics.SurveyEngine.addOnload(function()
{
// Next two lines read the value of the slider;
var question_text = "${q://QID1/ChoiceNumericEntryValue/1}";
var slider_value= parseInt(question_text);

// Performs some simple math operations on the value of the slider;
// (doubling it and adding a small random value);
var for_output = slider_value * 2 + Math.random();

// Writes the result into the EmbeddedData variable named "Output";
Qualtrics.SurveyEngine.setEmbeddedData("Output",for_output);
});

 
– Eric DeRosia