Change embedded data based on which choice is selected | XM Community
Skip to main content

Ive made an embedded data in the survey flow, now i want to change its value based on which choice was selected in one of the questions. The choices are  (Yes, Maybe, No). i want to change the embedded data value to “text1” if they choose Yes or Maybe and change it to “text2” if they choose No.

This is what i tried so far. Sorry, I am new to java and programming in general

 

  if("${q://QID949/ChoiceGroup/SelectedChoices}" == "Yes" || "Maybe")
    {
        Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text1")
    }
    else
    {
        Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text2")
    }

You can do this with branches in the survey flow. For example:

Branch: If Q1 Is Equal to Yes

Or If Q1 Is Equal to Maybe

    Set Embedded Data: vinnustadur = text1


Hi @joklinn ,

I will advise you to use @TomG  survey flow method .

But if you are still looking for  JS code then you can you use below code:

 

Qualtrics.SurveyEngine.addOnReady(function()
{
var selCho = "${q://QID949/ChoiceGroup/SelectedChoices}".split(",").map(function(choice) {
return choice.trim();
});

if (selCho.includes("Yes"))|| selCho.includes("Maybe")) {

Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text1");
}
else {

Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text2");
}





});


Hope it resolves your query😊!!


You can do this with branches in the survey flow. For example:

Branch: If Q1 Is Equal to Yes

Or If Q1 Is Equal to Maybe

    Set Embedded Data: vinnustadur = text1

problem with this is that I will loose the back button on the next question


Hi @joklinn ,

I will advise you to use @TomG  survey flow method .

But if you are still looking for  JS code then you can you use below code:

 

Qualtrics.SurveyEngine.addOnReady(function()
{
var selCho = "${q://QID949/ChoiceGroup/SelectedChoices}".split(",").map(function(choice) {
return choice.trim();
});

if (selCho.includes("Yes"))|| selCho.includes("Maybe")) {

Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text1");
}
else {

Qualtrics.SurveyEngine.setEmbeddedData('vinnustadur', "text2");
}





});


Hope it resolves your query😊!!

Hi, thanks for the answer,

This doesn’t seem to be working for me :/  Its supposed to display “text1” in the following question(if Yes or Maybe) but it just seemingly randomly chooses between “text1” and “text2” no matter what choice i choose.

I also tried putting this code inside addOnUnload on the question. Also tried putting it in addOnLoad on the following question, neither of which worked.


Ok, I’ve realized it isn’t random, it just seems to not update the embedded data on the first time I go to the next question. If I go to the next question and press back then again to the next question, it works. But I will of course need it to work the first time. Not sure what is happening here. I tried putting the following question into another block, still doesn’t work.


Ok, I’ve realized it isn’t random, it just seems to not update the embedded data on the first time I go to the next question. If I go to the next question and press back then again to the next question, it works. But I will of course need it to work the first time. Not sure what is happening here. I tried putting the following question into another block, still doesn’t work.

Use addOnPageSubmit and do it by choice id:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
    var cid = jQuery("#"+this.questionId+" input:checked").attr("choiceid");
if(cid=="1"||cid=="2") Qualtrics.setEmbeddedData("vinnustadur","text1");
else if(cid=="3") Qualtrics.setEmbeddedData("vinnustadur","text2");
});

 


Ok, I’ve realized it isn’t random, it just seems to not update the embedded data on the first time I go to the next question. If I go to the next question and press back then again to the next question, it works. But I will of course need it to work the first time. Not sure what is happening here. I tried putting the following question into another block, still doesn’t work.

I am assuming  that question is on the same page where you are applying the JS  and that's why it is not updating the value on first click.
As this variable(${q://QID949/ChoiceGroup/SelectedChoices}) will not have data until you click next button.(That’s the shortcoming of embedded data)
You will have to apply this JS after the question with id  QID949 .

 

 


Leave a Reply