Setting Embedded Data Using If/Else Statement in JavaScript | XM Community
Skip to main content

I am trying to set embedded data fields using an if/else statement in JavaScript. I tried to come up with the correct code, but I am new to JS and what I've come up with (shown below) isn't working; the fields don't get populated.
I have created the embedded data fields in the beginning of the survey flow, so I don't think that's the issue.
Qualtrics.SurveyEngine.addOnLoad(function() {
   if("${q://QID14/SelectedChoicesRecode}"!="1") 
   {Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_Yes", "X");
Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_No", "")}

  else if("${q://QID14/SelectedChoicesRecode}"!="2") 
  {Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_No", "X");
   Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_Yes", "")};
       
});

I know I can achieve the same result using branching in the survey flow, but it messes with the ability to use the Back button when taking the survey, which has to be available on this survey, so I'm looking for a JavaScript solution.

The code looks correct. But its important to know on which question you have added this code. Since, you have this code on "onLoad" function so this javascript code should be added on following question.
For example: based on Q1 response you want to assign value to embedded data then you will have to put this JS code on Q2 question and the embedded data will capture the value after "Next" button got clicked at Q2. Also, there should be a page break between Q1 and Q2
Please note, the same thing can be achieved by having the JS code on Q1 however, the function to execute this code will be different.
You can check this by piping the value of embedded data on "descriptive text" question.


You'll have to enter this code in the "addOnPageSubmit" section.
You can find a working example here:https://github.com/tafakkur/QualtricsHub/tree/master/JavaScript%20Files


Leave a Reply