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

Setting an embedded variable based on a hidden question... javascript mistake?

  • Feb 22, 2022 2:00 PM UTC
  • 6 replies
  • 143 views

MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Level 5 ●●●●●

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('~')[2];
        if (choiceNum == 2)
        {
       Qualtrics.SurveyEngine.setEmbeddedData("EndLoop", choiceNum);
        }
    }
}
});

Best answer by TomG

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"));
})

View original

6 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Feb 22, 2022 2:10 PM UTC

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();
});


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • Feb 22, 2022 2:15 PM UTC

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • Feb 22, 2022 2:38 PM UTC

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"));
})


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • Feb 22, 2022 4:07 PM UTC

Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • Feb 22, 2022 5:21 PM UTC

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);


}
});


MikeW
Level 5 ●●●●●
Forum|alt.badge.img+14
  • Author
  • Level 5 ●●●●●
  • Feb 22, 2022 5:24 PM UTC