Can someone tell me where I’m going wrong?
Context:
I’m trying to use java script in two multiselect multiple choice questions to add a value to an embedded data field. In the first question (“What do we do well?”) the embedded data fields should all be set to 0, and then each of the selected choices (e.g. Pay, Vacation, Workload, etc.) should turn their corresponding embedded data field to 1. In the second question (What do we do poorly?”), each selected choice (same choices, but without those previously selected) should set the corresponding embedded data field to -1.
I’m using the following java script on each question. At the moment, all my embedded data fields keep coming back 0 regardless what I answer in the survey.
Question 1 java script:
Qualtrics.SurveyEngine.addOnload(function() {
// Get the question ID for "Good stuff"
var questionId = this.getQuestionInfo().QuestionID;
// Get the selected choices for the question
var selectedChoices = this.getSelectedChoices();
// Initialize embedded data fields to 0
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", 0);
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", 0);
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", 0);
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", 0);
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", 0);
// Set the embedded data fields to 1 based on the selected choices
for (var i = 0; i < selectedChoices.length; i++) {
var choice = selectedChoices if (choice == 1) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", 1);
} else if (choice == 2) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", 1);
} else if (choice == 3) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", 1);
} else if (choice == 4) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", 1);
} else if (choice == 5) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", 1);
}
}
});
Question 2 Java Script
Qualtrics.SurveyEngine.addOnload(function() {
// Get the selected choices for the current question
var selectedChoices = this.getSelectedChoices();
// Set the embedded data fields to -1 based on the selected choices
for (var i = 0; i < selectedChoices.length; i++) {
var choice = selectedChoiceshi];
if (choice == 1) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Pay", -1);
} else if (choice == 2) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Vacation", -1);
} else if (choice == 3) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Workload", -1);
} else if (choice == 4) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Culture", -1);
} else if (choice == 5) {
Qualtrics.SurveyEngine.setEmbeddedData("Welcome - Benefits", -1);
}
}
});