Using javascript to read and update embedded data field | User case: Loop and Merge | XM Community
Skip to main content

Hi,

 

When I use Loop and Merge in Qualtrics, I like to capture the ordering of the loop and merge for easy analysis. I also like to dynamically update (and print to the participant) the Loop & Merge round number.

 

I’ve been using this code for a while but it suddenly does not work anymore. Does anybody know why that may be?

Thank you so much for your time and inputs!

 

(Note that these will go up to 12 for the current sandbox version, I have added up to 3)

 

The Loop & Merge page which contains the round number (which should be updated dynamically) and the javascript.
Loop & Merge Field 1

 

And the code in written form:

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
//input
var round = parseInt("${e://Field/round}");

var id = parseInt("${lm://Field/1}");
Qualtrics.SurveyEngine.setEmbeddedData("order_treatment_"+id, round);

// order opposite:
var id = parseInt("${lm://Field/1}");
Qualtrics.SurveyEngine.setEmbeddedData("treatment_of_scenario_nr_"+round, id);

// up round
var round = round + 1;
Qualtrics.SurveyEngine.setEmbeddedData("round", round);

});

Qualtrics.SurveyEngine.addOnReady(function()
{
      /*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
      /*Place your JavaScript here to run when the page is unloaded*/
      // order



});

 

I’m not sure why it isn’t working, but it could be simplified.  You don’t need the embedded field round, and you only need to set id once.

Qualtrics.SurveyEngine.addOnload(function() {
//input
var round = parseInt("${lm://CurrentLoopNumber}");
var id = parseInt("${lm://Field/1}");
Qualtrics.SurveyEngine.setEmbeddedData("order_treatment_"+id, round);

// order opposite:
Qualtrics.SurveyEngine.setEmbeddedData("treatment_of_scenario_nr_"+round, id);

});

 


Thank you so much, Tom!
I like your simplifications. I also tested out your code but it is not working. This makes me think that it’s not the code but something underlying that’s not working as expected. 
I appreciate your input and validation of my code!


@cella80 ,

The only thing I can think of is ${lm://Field/1} not parsing for some reason.  Make sure you don’t have any extra invisible characters in there and check the console for errors.  


Leave a Reply