Javascript for survey logic and embedded data variables | XM Community
Skip to main content

Hello!
I am trying use Javascript to write survey logic on embedded data variables. To break that down, survey participants will be asked to choose between option A or option B in question 1. For the next question, they will be asked to choose between option C or option B OR option D or option B, and they will be shown C or D depending on their choice in the first question.
I have this working between blocks using survey logic, but I need it to work within a block:
pic1.png
but I want this to work within a block (between questions). So in the following block, I need Javascript code that determines the value of littleSS2 in Q2 based on the option chosen in Q1. For example, if you choose the first option in Q1, then littleSS2 = 50; however, if you choose the second option in Q2, then littleSS2 = 100.
pic2.png
Does that make sense? What kind of Javascript code should go in Q2 that would take care of this?
Thank you for your help!

Hi there,
To set an Embedded Data field within a block, you will want to create the Embedded Data field at the top of the survey flow.
Then, you can use addOnPageSubmit to set the Embedded Data Value when the next button is selected and a certain survey answer is selected, like in the below, adapted from a "Leave Loop and Merge Early" thread. A QSF for that is also below if it helps.
Qualtrics.SurveyEngine.addOnPageSubmit(function (type) { // To record the selection when the page is submitted

if (type == "next") {

var selChoice = this.getSelectedChoices();


            // Set Embedded Data
            
if (selChoice == 4) {
Qualtrics.SurveyEngine.setEmbeddedData("testvariable", "TRUE");
            } else
                Qualtrics.SurveyEngine.setEmbeddedData("testvariable", 1);


}
});
LoopMergeExit.qsf


Leave a Reply