embedded data loop-merge for custom javascript | XM Community
Question

embedded data loop-merge for custom javascript

  • 22 January 2019
  • 4 replies
  • 11 views

Badge
Hi, I have managed to get this table as response format for qualtrics using the below HTML"

!


<style type="text/css">
table, th, td {
border: 3px solid white;
font-size: 14px;
border-collapse: collapse;
padding:8px
}
</style>
<table width="100%">
<tbody>
<tr align="center">
<td bgcolor="#FF0332"><input id="0" name="0" type="radio" value="0">0</td>
<td bgcolor="#cb3333"><input id="13" name="13" type="radio" value="13">13</td>
<td bgcolor="#cb3333"><input id="25" name="25" type="radio" value="25">25</td>
<td bgcolor="#cb6533"><input id="38" name="38" type="radio" value="38">38</td>
<td bgcolor="#cb6533"><input id="50" name="50" type="radio" value="50">50</td>
<td bgcolor="#cb6533"><input id="63" name="63" type="radio" value="63">63</td>
<td bgcolor="#97cb38"><input id="75" name="75" type="radio" value="75">75</td>
<td bgcolor="#97cb38"><input id="88" name="88" type="radio" value="88">88</td>
<td bgcolor="#63fe3c"><input id="100" name="100" type="radio" value="100">100</td>
</tr>
<tr align="center">
<td bgcolor="FF0332">No Connection</td>
<td bgcolor="#cb3333" colspan="2">Remote Association</td>
<td bgcolor="#cb6533" colspan="3">Moderate Association</td>
<td bgcolor="#97cb38" colspan="2">Strong Association</td>
<td bgcolor="#63fe3c">Brings Immediately to mind</td>
</tr>
</tbody>
</table>


I have included a loop-merge in the question where in every loop a word-pair is selected from a list. To capture the response, I have used the following javascript code in Qualtrics:

Qualtrics.SurveyEngine.addOnload(function() {
$('0').observe('click', function(event) {
var val1 = "0";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('13').observe('click', function(event) {
var val1 = "13";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('25').observe('click', function(event) {
var val1 = "25";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('38').observe('click', function(event) {
var val1 = "38";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('50').observe('click', function(event) {
var val1 = "50";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('63').observe('click', function(event) {
var val1 = "63";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('75').observe('click', function(event) {
var val1 = "75";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('88').observe('click', function(event) {
var val1 = "88";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});
$('100').observe('click', function(event) {
var val1 = "100";
Qualtrics.SurveyEngine.setEmbeddedData("a1", val1);
});

});

However, in my data output, I do not get the response (embedded data "a1") for the seperate loop trials.

Can someone help me with the code please and tell me what i am doing wrong?

Thanks you

4 replies

Userlevel 5
Badge +6
Hi @e0046902 ,
in case your question is a Qualtrics built-in multiple choice question, Qualtrics should store your responses automatically.
In other cases, I would add the new value with a separator to my variable. Then you would have one string containing all your values. Be aware, if your questions are randomized, the values will be in the order of particular randomization. Therefore you should store also the order of your words. Here is a nice manual on how to do it. You can store also your responses in a similar way. I can't be more specific since I don't know how exactly is your question designed.
Userlevel 7
Badge +27
e0046902,

You are overwriting the embedded variable a1 each time through the loop, so the only value saved is the one from the last loop. If you are going to use embedded variables, you need a different one for each loop.
Badge
HI @fleb

Thanks for the link. So yes I have used that technique mentioned in the blog, and its working pretty ok in getting a string of responses separated by a specific delimiter. But the catch is that the last response (from the last iteration of the loop) is always not recorded. I am not sure why this is the case but I am guessing it has something to do with placing my javascript as addOnLoad?
Userlevel 5
Badge +6
Hi @e0046902 ,
I think you're right. I've never tried to place it in the addOnLoad, but it probably always stores the previous value when the new iteration page is loaded. No new iteration page is loaded after the last iteration, so the last value is missing. Try to place your code into the addOnPageSubmit function. It is not predefined in Qualtrics, but you can create it in the same way as predefined ones.

Leave a Reply