Using Javascript to save embedded variables within survey | XM Community
Skip to main content

Hello everyone! This time last year, I asked a question about using Javascript to save selections from a multiple choice question as separate variable. I got some great suggestions and the survey worked like a dream. JS to set embedded variables not saving on page submit

The tricky part was that I needed those variables to work on the very next page. Here is the code that I used:

 

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(SchoolNumberSelectNi])); break; case 1: Qualtrics.SurveyEngine.setEmbeddedData("School2", that.getChoiceVariableName(SchoolNumberSelectNi])); break; case 2: Qualtrics.SurveyEngine.setEmbeddedData("School3", that.getChoiceVariableName(SchoolNumberSelectNi])); break; case 3: Qualtrics.SurveyEngine.setEmbeddedData("School4", that.getChoiceVariableName(SchoolNumberSelectNi])); break; case 4: Qualtrics.SurveyEngine.setEmbeddedData("School5", that.getChoiceVariableName(SchoolNumberSelectNi])); break; } } 

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

    })

});

 

Sadly, this code no longer works. I do not know why. The values and related logic all exist in the export of the survey, but I can’t use those fields in the survey itself, even with pages in between the question where the variable was set and where the variable is being used. This breaks the entire survey.

Has anyone run into this? Better yet, any suggestions?

@erinkru Hi, it’s me who provide you the answer a year ago. I’ve been able to replicate the problem, somehow all the setembeddeddata function refuse to work inside the loop and next button event listener. Only God know why. So looks like we gonna have to go back to the basic

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var that = this;

var SchoolNumberSelect = that.getSelectedChoices();
console.log("Selected Choices:", SchoolNumberSelect);

for (var i = 0; i < SchoolNumberSelect.length && i < 5; i++) {
var choiceVariableName = that.getChoiceVariableName(SchoolNumberSelectSi]);
console.log("Setting School" + (i + 1) + ":", choiceVariableName);
Qualtrics.SurveyEngine.setEmbeddedData("School" + (i + 1), choiceVariableName);

console.log("Setting SchoolNumber" + (i + 1) + ":", SchoolNumberSelectSi]);
Qualtrics.SurveyEngine.setEmbeddedData("SchoolNumber" + (i + 1), SchoolNumberSelectSi]);
}
});

Hope this help


Leave a Reply