Using JavaScript to set embedded data from multiselect multiple choice question | XM Community
Skip to main content

Hi all,

I am trying to set an embedded data variable from the selected choices on a multiselect multiple choice question using JavaScript.

I can’t use piped text as we have a default value set at the beginning of the survey flow, and can’t set the embedded data in survey flow as we need the back button to be active.

I have the JavaScript working for the single select multiple choice question as below:

jQuery("select").on('click', function(){
var temp = jQuery("#QID59 option:selected").text();
Qualtrics.SurveyEngine.setEmbeddedData('variable', temp);
});

But this doesn’t work for my multiselect question. I have tried SO many things and nothing seems to work! This gets me the closest:

Qualtrics.SurveyEngine.setEmbeddedData('variable', "${q://QID52/ChoiceGroup/SelectedChoices}");

But for some reason you have to go back and forward between pages a couple times, which would be infuriating for the end user so is not an option.

I suspect the thing I’m missing is the correct selector/s for this question type. Can anyone help, or at least point me in the right direction?

 

Thanks in advance.

Hi @AJProut ,

I have implemented above request using :

document.getElementById("NextButton").addEventListener("click" , function (){
let ques = document.getElementById("QID59 ");
let selectedOptions = ques.querySelectorAll(".ChoiceTextPositionLeft.q-checked");
let optionsText = "";

for (let i = 0; i < selectedOptions.length; i++) {
optionsText += selectedOptionssi].innerText + ", ";
}

optionsText = optionsText.slice(0, -2);

Qualtrics.SurveyEngine.setEmbeddedData('variable', optionsText);
});

It stores the values  in optionsText and then finally it is stored in embedded data “variable”.
Hope it resolves your query😊!!


Hi @AJProut ,

Just to add you can implement same using Survey flow.
Just make this multi select question as your  last question in the block.
And then you can  store the selected choices using  “${q://QID52/ChoiceGroup/SelectedChoices}”  in the survey flow.
And can call it anywhere in the survey from then onwards by piping the embedded data .
Hope this resolves your query😊!!


Hi @qualtrics_nerd 

As specified in the question adding it to the survey flow isn’t an option. This is already the last question in the block and we need the back button enabled between this and the next (last) block, which is where the embedded data is displayed in a confirmation page before submission.

 

I have tried your method and unfortunately not had any success. We will keep trying other options in the meantime.


If you need to save the selections as Embedded Data, adding the below to the OnReady section of a question that follows the Multiselect will save the selections to an Embedded Data field "choiceText". 

var choiceText = "${q://QID52/ChoiceGroup/SelectedChoices}";
Qualtrics.SurveyEngine.setEmbeddedData("choiceText",choiceText);

If you need to save the selections as Embedded Data on page submit, the below can be added to the bottom of the Multiselect's JavaScript:

Qualtrics.SurveyEngine.addOnPageSubmit(function() 
{

var selChoice = this.getSelectedChoices();
var choiceRef = "";
var choiceComma = "";
var choiceText = "";
for (let i = 0; i < selChoice.length; i++) {
choiceRef = "#" + this.questionId + "-" + selChoiceei] + "-label > span";
choiceComma += document.querySelector(choiceRef).innerHTML + ", ";
}

choiceText = choiceComma.replace(/,\s*$/, "");

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

});

 


Hi @AJProut ,

Please find below updated code:

 

Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
document.getElementById("NextButton").addEventListener("click" , function (){
let ques = document.getElementById("QID140");//update QID here
let selectedOptions = ques.querySelectorAll(".ChoiceTextPositionLeft.q-checked");
let optionsText = "";

for (let i = 0; i < selectedOptions.length; i++) {
optionsText += selectedOptionsni].innerText + ", ";
}

optionsText = optionsText.slice(0, -2);
console.log(optionsText);
Qualtrics.SurveyEngine.setEmbeddedData('time', optionsText);
});

});

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

});

Just to confirm ,are you adding the QID of your question in above code?
Further are there any more question that page ?
As it is working fine at my end.

Hope this resolves your query😊!!
 


Thank you @qualtrics_nerd and @Tom_1842 for your help!

We’ve finally found success with Tom’s second option. The first was still making us load the page twice before showing but updating the embedded data on page submit seems to have done the trick.

 

Thanks again


Leave a Reply