Setting an embedded variable based on a hidden question... javascript mistake? | XM Community
Skip to main content

I'm setting an Embedded Variable based on a question.... however, I've used default choices to preselect the response and then hidden the question. Does the code above only work when a response is "clicked" - and if so, do I need to change this.questionclick to something else?

Qualtrics.SurveyEngine.addOnload(function ()
{

 //jQuery("#"+this.questionId).hide();

    this.questionclick = function(event,element)
    {
        console.log(event,element);
        if (element.type == 'radio')
    {
        var choiceNum = element.id.split('~')e2];
        if (choiceNum == 2)
        {
       Qualtrics.SurveyEngine.setEmbeddedData("EndLoop", choiceNum);
        }
    }
}
});

Yes, the code will only work when a response is clicked. If you have a default choice and the question is hidden with JS, you don't need to set an embedded data field since it is the same as the question's answer. All you need is:
Qualtrics.SurveyEngine.addOnload(function () {
jQuery("#"+this.questionId).hide();
});


Thanks Tom
I'm setting the embedded variable as I'm using that embedded variable to end a loop and merge early (my loop and merge questions are only shown when endloop = 1) - but I'm only ending the loop early for certain respondent types. Does that make sense?
So, basically for certain respondent types I want to set endloop to 2 after the 3rd loop in my loop and merge block.


https://community.qualtrics.com/XMcommunity/discussion/comment/43884#Comment_43884You can do this:
Qualtrics.SurveyEngine.addOnload(function () {
var q = jQuery("#"+this.questionId);
q.hide();
Qualtrics.SurveyEngine.setEmbeddedData("EndLoop",q.find("input:checked").attr("choiceid"));
})


https://community.qualtrics.com/XMcommunity/discussion/comment/43885#Comment_43885Perfect - thanks Tom


Hi there, neat implementation and Tom's method of hiding the question is the way to go. If it helps, another approach for ending the loop early is to check for answer values upon page submit
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("EndLoop", "TRUE");
            } else
                Qualtrics.SurveyEngine.setEmbeddedData("EndLoop", 1);


}
});


https://community.qualtrics.com/XMcommunity/discussion/comment/43896#Comment_43896Thanks Tom


Leave a Reply