Saving Pick, Group, & Rank responses to Embedded Data with Javascript | XM Community
Solved

Saving Pick, Group, & Rank responses to Embedded Data with Javascript

  • 23 June 2021
  • 2 replies
  • 62 views

Badge

In the Survey flow, I've set up Group1a, Group1b, & Group1c as embedded data fields
I created a Pick, Group, & Rank item where the participant is to drag and drop their top three (of six) choices into a single group.
All I want to do is save the selected choices to the embedded data fields so I can use them later.
I'm really new to Javascript. I wrote the code below, which literally doesn't seem to do anything. I made another item which is multiple choice where I pipe into the options text the embedded data fields. When running through a preview, all three of the multiple choice option texts are blank. Any help would be appreciated.
I wrote the following code into the Pick, Group, & Rank item
Qualtrics.SurveyEngine.addOnUnload(function()
{
              /*Place your JavaScript here to run when the page is unloaded*/          
              let choicesall = this.getSelectedChoices();
              let choicesarray = choicesall.split(",");     
              Qualtrics.SurveyEngine.setEmbeddedData('Group1a', choicesarray[0]);
              Qualtrics.SurveyEngine.setEmbeddedData('Group1b', choicesarray[1]);
Qualtrics.SurveyEngine.setEmbeddedData('Group1c', choicesarray[2]);
});

icon

Best answer by ChiragK 25 June 2021, 13:06

View original

2 replies

Badge +8

Hello, Try following code, hope that helps.
Qualtrics.SurveyEngine.addOnReady(function () {
    var $this = jQuery(this.questionContainer);

    var $groupUl;
    jQuery(".QuestionBody .groupsContainerTd .Group", $this).each(function (i) {
        $groupUl = jQuery(this).find("ul.ui-sortable");
        $groupUl.sortable({
            update: function (event, ui) {
                Qualtrics.SurveyEngine.setEmbeddedData('Group1a', "");
                Qualtrics.SurveyEngine.setEmbeddedData('Group1b', "");
                Qualtrics.SurveyEngine.setEmbeddedData('Group1c', "");

                jQuery("li", $groupUl).each(function (i) {
                    if (i === 0) {
                        Qualtrics.SurveyEngine.setEmbeddedData('Group1a', jQuery(this).find("label[for]").text().trim());
                    } else if (i === 1) {
                        Qualtrics.SurveyEngine.setEmbeddedData('Group1b', jQuery(this).find("label[for]").text().trim());
                    } else {
                        Qualtrics.SurveyEngine.setEmbeddedData('Group1c', jQuery(this).find("label[for]").text().trim());
                    }
                });
            }
        });
    });
});

Badge

Thanks Chirag, works like a charm

Leave a Reply