Using JS to Embed Data in Individual Items/Response Options | XM Community
Skip to main content

My goal is to randomize 10 items within a block, and then ask a series of questions about the last item displayed. I've spoken to Qualtrics support, and they said this wasn't possible on the platform. I figured out how to do this with JS, but it is only working for the item stem, not the response option that was selected.
I begin by defining the embedded data at the beginning of the survey.

image.png
Next, I add JS for each question in the first block. Labeling them with the same embedded name causes Qualtrics to overwrite the embedded data each time, which means that it will remember the last question displayed from the first block:
JS for Q1 (note that it calls QID1):
Qualtrics.SurveyEngine.addOnload(function()
{

var text = "${q://QID1/QuestionText}";

Qualtrics.SurveyEngine.setEmbeddedData( 'TestQuestion', text );

var answer = "${q://QID1/ChoiceGroup/SelectedChoices}";

Qualtrics.SurveyEngine.setEmbeddedData( "TestAnswer", answer );


});
JS for Q2 (note that it calls QID2):
Qualtrics.SurveyEngine.addOnload(function()
{

var text = "${q://QID2/QuestionText}";

Qualtrics.SurveyEngine.setEmbeddedData( 'TestQuestion', text );

var answer = "${q://QID2/ChoiceGroup/SelectedChoices}";

Qualtrics.SurveyEngine.setEmbeddedData( "TestAnswer", answer );


});
Next, I call the embedded data in the subsequent block in the questions:
image.png
However, when I launch the survey, it only displays the TestQuestion metadata:
image.pngWhat am I doing wrong? How can I get the response option (TestAnswer embedded data) to pipe in? Should it be added at a different spot in the JS?

I also tried the following (moved the JS to add on page submit), but it didn't work either:
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")

{var answer = "${q://QID2/ChoiceGroup/SelectedChoices}";

Qualtrics.SurveyEngine.setEmbeddedData( "TestAnswer", answer );}

});


Piped field are resolved before the page loads so you are piping the selected choice before the question has been answered. Assuming this is a single answer multiple choice question, you can get the selected choice text like this:
Qualtrics.SurveyEngine.addOnPageSubmit(function(type) {
var answer = jQuery("input:checked").closest("li").find(".LabelWrapper span").html();
Qualtrics.SurveyEngine.setEmbeddedData( "TestAnswer", answer );}
});



This makes it pipe in (thank you!) but it's piping in the answer from the first question presented rather than the last question shown. Any idea how to make it pull from the last one presented? I'm not sure why it isn't overwriting.
ETA: I think I figured it out. Does this look right to you, or do you think it will cause problems?
I added

.last()
after
find(".LabelWrapper span")
:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {

var answer = jQuery("input:checked").closest('li').find(".LabelWrapper span").last().html();

Qualtrics.SurveyEngine.setEmbeddedData( "TestAnswer", answer );

});


https://community.qualtrics.com/XMcommunity/discussion/comment/44086#Comment_44086I assumed you were showing one question per page. Otherwise, you are relying on timing in your original approach which could lead to erratic results. If have them on the same page you are running the script for each question simultaneously.
Anyway, if you are going to do that way, your last() is in the wrong place. It should be it should be:
var answer = jQuery("input:checked").last().closest('li').find(".LabelWrapper span").html();
It will still run for each question, but they will all result in the same answer.


Yes, same page. The goal is just to grab the text from the last item that is displayed and the text from the response option selected for that last item.
I tried it both ways (with

last()
in both spots), and I got the same result (e.g., the response option for the last item displayed was the one shown in the following block, even if I answer the items out of order - this is good).
What did you mean when you said "they will all result in the same answer"?
Is there anything else for me to look out for?


What did you mean when you said "they will all result in the same answer"?

I meant that if you add the script to all the questions on the page it will run multiple times - once for each question. Since all the questions are on the same page, you only need to add the script to one question if you do it the way I recommended with the last() function after the input selector. With last() after the label selector you must run the script on every question, and you are counting on the script of the last question running last and updating the values set by the other instances...although it may work in your testing, it isn't really the correct way to do it.


TomG Fascinating - thank you so much! I would not have figured this out without your help.


Leave a Reply