Skip to content

2. Advice on JavaScript

Matt Andreotta edited this page Mar 30, 2017 · 1 revision

Advice for Adding JavaScript and HTML

How to access and write values to an embedded field at any point in the survey

The following is ruthlessly stolen from Williams and Moldaonado's website:

Access

To access embedded data at any time using JavaScript: simply set a variable to be equal to the piped text required to access to embedded data in survey questions (as a string). For example:

var myData = "${e://Field/myEmbeddedData}"

Write

To write a new value to an embedded data field, use the call:

Qualtrics.SurveyEngine.setEmbeddedData('fieldName', newvalue);
//For example, to change the value of the embedded data field myEmbeddedData to be equal to 10, write:
Qualtrics.SurveyEngine.setEmbeddedData('myEmbeddedData', 10);

Other commands

A few other useful commands exist to handle embedded data fields, such as:

addEmbeddedData('name', value); //adds embedded data.
setEmbeddedData('name',value); //sets the embedded data.
getEmbeddedData('name'); //returns the embedded data.

Tracking the order of loops in a Qualtrics loop & merge block

Once again, shamelessly quoting the work of Moldanado (2013) concerning Qualtrics' Loop & Merge feature

Loop & Merge blocks basically offer you a way to ask the same question multiple times, with variables within the question changing every time it is asked. For instance, in one recent experiment I helped program, we wanted to ask participants to make judgments about several students. We used a loop and merge to set the names, course types, and other information of the students so we could ask about many students using the exact same wording and without having to copy the question over and over again. You can even paste cells directly from an Excel spreadsheet into the Loop & Merge, so using formulas to create appropriate data was a breeze.

Loop & Merge is pretty neat, but it has a serious flaw: it doesn't record the order of randomized loops.

Recording random loops

So, Loop & Merge blocks have the option to turn on randomization of the loop order, which is great – in our research, it tends to be our default. However, we run into an issue: the order in which loops are presented isn't recorded! This is a problem of course – as any psych student can tell you, ordering effects can be important. So how can we record the ordering of the loops to make sure the order in which we present our materials isn't affecting our results? The answer is threefold:

  1. An embedded data field for each Loop & Merge block we want to record
  2. A unique identified for each loop in the Loop and Merge. We typically just use a number (though in our implementation this gets hairy if we have large numbers of items)
  3. Some special JavaScript, including a call using one of Qualtrics' methods

Items one and two are straightforward: create an embedded data field, and an extra loop & merge field to identify each loop. For this example, our embedded data field will be called "preorder" (for recording the order of a pretest) and we will use the first field of the Loop & Merge as our identifier. Now, on to the JavaScript, which I've tried to comment well enough to make each line understandable (in JavaScript, single line comments begin with "//"). I added it using the "Add JavaScript" option available on every question.

/*Place Your Javascript Below This Line*/
   var questionText = "${lm://Field/1}"; // "${lm://Field/1}" will actually evaluate to  
   //whatever is Field 1 in the current Loop & Merge loop.
   // You can do this with embedded data too, as seen in the next line
   var order = "${e://Field/preorder}"+ questionText; // gets the value of the embedded data
   // field "preorder", concatenates the current loop's identifier to it,
  //and stores that as a variable
   Qualtrics.SurveyEngine.setEmbeddedData('preorder', order); // updates the  
   //embeddeddata field "preorder" to be our order variable, which has the current loop's
   //identifier attached, effectively constructing a string of numbers representing the order

This code is run every time the page containing it is loaded (e.g. on every loop of our Loop & Merge). It takes the order as it currently is, adds the current loop to the order, and then passes that back to the embedded data field so it is stored. Voila! Your survey will now track the order in which items in a Loop & Merge are presented!

One issue Maldonado points out:

this implementation will give you a string of numbers, like "7153264". We decided on this so that our statistical package wouldn't have to deal with delimiters. however, this becomes tricky when you have more items, and it becomes almost impossible to tell whether, say, the substring "21" represents item 2 then item 1, or item 21. It would be easy to add delimiters though; to use the pipe character, |, as a delimiter, you would just modify var order = "${e://Field/preorder}"+ questionText; to be var order = "${e://Field/preorder}"+ "|" + questionText;, and of course you can change the delimiter character to be anything you'd like.

References

Maldonado, S. (2013). Tracking the order of loops in a qualtrics loop & merge block. Retrieved from: http://sgmaldonado.com/main/tracking-order-qualtrics-loop-merge.

Qualtrics (2017). Loop & Merge. Retrieved from: https://www.qualtrics.com/support/survey-platform/survey-module/block-options/loop-and-merge/.

Williams, J. J., & Moldaonado, S. (2017). Advanced Qualtrics. Retrieved from: http://sites.cognitivescience.co/qualtrics/home/advanced-qualtrics.

Clone this wiki locally