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

LarryKaplan
LarryKaplan Boston, MA, USACommunity Member Qubie ✭
edited July 8 in Custom Code

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

});

Best Answer

  • ChiragK
    ChiragK GUJARATCommunity Member Guru ✭✭
    edited June 25 Accepted Answer

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

Answers