using javascript to create/visualize randomized embedded data in the same question | XM Community
Skip to main content
Hi all,

I'm looking for directions as I (have to) start using java. I'm programming an experiment where participants will be asked to repeatedly choose between two options called A and B.



What would you prefer between these two options?

Option A: xx amount of dollars at time [time level],

Option B: yy amount of dollars at time [time level], where the time level is randomly picked from five levels. Becas



where the time level of option A is randomly picked from three levels [now, 1 week from now, 2 weeks from now] and the time level of option B is randomly picked from five levels [1 week from now, ... , 5 weeks from now]. Furthermore, the time level of Option B must always be greater than the time level of Option B [e.g. if A=1week then B>1week]. This design ended up to be too complicated to be managed within the Qualtrics Survey Flow, so my only way out is to write some lines of java in each question. This Java script should create the five time levels as embedded data, randomly pick one level to be used in Option A and one in Option B (with time B level > time A level) and then I'd show these embedded data as piped text in the question.

I used all the previous contributions about this topic but it's not working the way I hoped. Specifically, when I use java I can't create and visualize an embedded data within the same question. The only way I can make it working is by creating a variable in, let's say, Question Q10 like this:

Qualtrics.SurveyEngine.addOnload(function()

{

Qualtrics.SurveyEngine.setEmbeddedData('time','one week from now');

});

and then, in the following question Q11, write the following:

Qualtrics.SurveyEngine.addOnReady(function()

{

Qualtrics.SurveyEngine.getEmbeddedData('time');

});

If I do this, in Q11 I can put time as piped embedded data in the question and see the "one week from now" text visualized.

I tried to put both .set and .get instructions in the same script same question but in this way nothing is visualized in the question.

Any way that I can just put .set and .get lines together and get the variable visualized all in the same question?

Thank you very much for any help you may give.
Hello @calmac ,



We cannot set and get the embedded data on the same page, instead you can change the choice text using js
Hi Shashi,

thank you for your reply, still I'm not sure how you think this should be used to address my problem. What do you mean, if possible can you please articulate a bit more?
> @calmac said:

> Hi Shashi,

> thank you for your reply, still I'm not sure how you think this should be used to address my problem. What do you mean, if possible can you please articulate a bit more?



@Shashi means you can't use embedded data. Instead of assigning your random time levels to embedded variables, assign them to JS variables. Then instead of piping embedded variables, update the html with JS. So your choices (html) would be something like this:

```

xx amount of dollars at time <span id="aTime"></span>

yy amount of dollars at time <span id="bTime"></span>

```

Then, assuming you assign your random time levels to JS variables aTime and bTime you would update the choices with JS as follows:

```

jQuery("#aTime").html(aTime);

jQuery("#bTime").html(bTime);

```
Thank you very much @TomG, this elaborates on Shashi suggestion enough for me to understand. I'll go and study how to implement this (make conditional works with string will be tricky I guess).

One last question, it's more like a confirmation, if you don't mind. If I use the js+html and no embedded data to create and manage variables I need, I can still find all these variables and participant responses later on in the results when I download the survey data, correct?

Thanks again @Shashi and TomG.
> @calmac said:

> One last question, it's more like a confirmation, if you don't mind. If I use the js+html and no embedded data to create and manage variables I need, I can still find all these variables and participant responses later on in the results when I download the survey data, correct?

No, if you want them saved in your response data, you'll have to save them as embedded variables:

```

Qualtrics.SurveyEngine.setEmbeddedData("aTime", aTime);

Qualtrics.SurveyEngine.setEmbeddedData("bTime", bTime);

```

You also need to create the embedded variables in the survey flow prior to the block that updates their values.

Leave a Reply