JS to set embedded variables not saving on page submit | XM Community
Skip to main content

Context available in a previous post, but I am splitting the selected choices from a question into separate variables for use in later questions. This works when I place the code in the onReady section of Javascript on an in-between block; I can then use the embedded variables on the following page.

This works:
Multiple choice question I want to split and save > New block with random question and JS in onReady > Block using newly created embedded variables

This does not work:
Multiple choice question I want to split and save, with JS in onPageSubmit >  Block using newly created embedded variables

My understanding from community posts was that using onPageSubmit would save the variables when the next button was pressed, so I could use the saved variables on the next page, but it doesn’t work. The embedded fields are empty. Is there an issue with my code, or am I misunderstanding how this function works? Is there another way for me to do this that does not require the in-between page as a roadblock?

This works with onReady, but not addOnPageSubmit. I’ve tried it with and without the next.


Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{

var SchoolSelect = "${q://QID12/ChoiceGroup/SelectedChoices}";
var SchoolNumberSelect = "${q://QID12/SelectedChoicesRecode}";

var SchoolSelectArr = SchoolSelect.split(', ');
var SchoolNumberSelectArr = SchoolNumberSelect.split(', ');


for (var i = 0; i < SchoolSelectArr.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("School1", SchoolSelectArrSi]);
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("School2", SchoolSelectArrSi]);
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("School3", SchoolSelectArrSi]);
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("School4", SchoolSelectArrSi]);
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("School5", SchoolSelectArrSi]);
break;
}
}

for (var i = 0; i < SchoolNumberSelectArr.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber1", SchoolNumberSelectArrSi]);
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber2", SchoolNumberSelectArrSi]);
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber3", SchoolNumberSelectArrSi]);
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber4", SchoolNumberSelectArrSi]);
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber5", SchoolNumberSelectArrSi]);
break;
}

}

}
});

 

@erinkru Onpagesubmit run when you’re result are being saved by Qualtrics so the value is not populated yet that time. Try this code instead, it’s listen to the next button being clicked
 

Qualtrics.SurveyEngine.addOnReady(function()
{
var that = this;
var nextButton = document.getElementById('NextButton');
nextButton.addEventListener('click', function() {

var SchoolNumberSelect = that.getSelectedChoices();

for (var i = 0; i < SchoolNumberSelect.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("School1", that.getChoiceVariableName(SchoolNumberSelectSi]));
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("School2", that.getChoiceVariableName(SchoolNumberSelectSi]));
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("School3", that.getChoiceVariableName(SchoolNumberSelectSi]));
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("School4", that.getChoiceVariableName(SchoolNumberSelectSi]));
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("School5", that.getChoiceVariableName(SchoolNumberSelectSi]));
break;
}
}

for (var i = 0; i < SchoolNumberSelect.length; i++) {
switch (i) {
case 0:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber1", SchoolNumberSelectSi]);
break;
case 1:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber2", SchoolNumberSelectSi]);
break;
case 2:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber3", SchoolNumberSelectSi]);
break;
case 3:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber4", SchoolNumberSelectSi]);
break;
case 4:
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber5", SchoolNumberSelectSi]);
break;
}
}

})

});

Make sure you have recode value and variable naming turning on and a page break between 2 question
 

Hope this helps


@dxconnamnguyen Thank you! This does solve my sequencing problem.

Is it possible to keep the display name in School1-5 fields and the actual recode value in the SchoolNumber1-5 fields? The recode values are identifiers for the school (e.g. 0123 or 4321)  and are used in later logic as well as on the back end, but it’s recording the display sequence number instead. I’ve been poking around the API commands, but I’m out of my depth.

It’s worth it if I have to re-do the logic, but I would like to keep the recode values if at all possible.


@dxconnamnguyen Thank you! This does solve my sequencing problem.

Is it possible to keep the display name in School1-5 fields and the actual recode value in the SchoolNumber1-5 fields? The recode values are identifiers for the school (e.g. 0123 or 4321)  and are used in later logic as well as on the back end, but it’s recording the display sequence number instead. I’ve been poking around the API commands, but I’m out of my depth.

It’s worth it if I have to re-do the logic, but I would like to keep the recode values if at all possible.

@erinkru Feel free to change the recode value and variable as your need. These field is what will be saved in your data and by default is the displayed one. The picture is just example, I just want you to make sure that these fields don’t get messup (sometime if you delete a choice it will)


Leave a Reply