Inserting piped text from rank order question | XM Community
Skip to main content

In my survey participants will need to rank the following chocolate bars from most preferred to least preferred: Snickers, Mars, Twix, Bounty, Picnic, Kit Kat, Milky Way, Crunchie, Cherry Ripe, and Twirl. This is a rank order type question and the format of the question is drag and drop. After participants rank the chocolate bars from most preferred to least preferred I would like to display their two most preferred chocolate bars and three least preferred chocolate bars on the following screen. They will then need to choose one of these five bars. Here is what I have put down in the survey flow:

 

In my item ranking question I have included the following javascript:

Qualtrics.SurveyEngine.addOnload(function() {
    var rankQuestionId = 'QID14'; // Replace with the actual Question ID of the Rank Order question
    var rankData = t];

    jQuery("#" + rankQuestionId + " .draggable").each(function() {
        var choiceText = jQuery(this).find(".draghandle span").text();
        var ranking = jQuery(this).data("rank");

        rankData.push({ text: choiceText, rank: ranking });
    });

    // Sort the rankData array by ranking (ascending order)
    rankData.sort(function(a, b) {
        return a.rank - b.rank;
    });

    // Capture the rankings for the specified snacks
    var mostPreferredSnack = rankData0].text;
    var secondMostPreferredSnack = rankDatar1].text;
    var leastPreferredSnack = rankDatarankData.length - 1].text;
    var secondLeastPreferredSnack = rankData rankData.length - 2].text;
    var thirdLeastPreferredSnack = rankData rankData.length - 3].text;

    // Set the embedded data fields
    Qualtrics.SurveyEngine.setEmbeddedData('MostPreferredSnack', mostPreferredSnack);
    Qualtrics.SurveyEngine.setEmbeddedData('SecondMostPreferredSnack', secondMostPreferredSnack);
    Qualtrics.SurveyEngine.setEmbeddedData('LeastPreferredSnack', leastPreferredSnack);
    Qualtrics.SurveyEngine.setEmbeddedData('SecondLeastPreferredSnack', secondLeastPreferredSnack);
    Qualtrics.SurveyEngine.setEmbeddedData('ThirdLeastPreferredSnack', thirdLeastPreferredSnack);
});


Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
    /*Place your JavaScript here to run when the page is unloaded*/

});

 

I then insert piped text for the item selection screen (see below):

 

However, when I preview the survey, none of the options appear:

 

Can someone please assist?

 

Thanks in advance!

 

 

Hi @JLevyECON 
You can achieve this without JS as well but it will be a lengthy process. 
Create branch logic in the survey flow as following:
 

You need to do this for all the chocolates and for all the other ranks i.e., 2,3,4,5 (insert values as 2, 8,9,10 in the branch logic instead of 2,3,4,5 because you have total of 10 chocolates and the least 3 preferred ranks will be 8,9,10).


Then create a mcq question with the following pipe text.
 


This process works perfectly.


Ji @JLevyECON ,

Add this to your rank order question js:

Qualtrics.SurveyEngine.addOnPageSubmit(function()
{

var rankData = this.getQuestionContainer().querySelectorAll("li");
console.log(rankData);
var mostPreferredSnack = rankDataa0].childrenn1].innerText;
var secondMostPreferredSnack = rankDataa1].childrenn1].innerText;
var leastPreferredSnack = rankDataarankData.length - 1].childrenn1].innerText;
var secondLeastPreferredSnack = rankDataarankData.length - 2].childrenn1].innerText;
var thirdLeastPreferredSnack = rankDataarankData.length - 3].childrenn1].innerText;
console.log(mostPreferredSnack);

Qualtrics.SurveyEngine.setEmbeddedData('MostPreferredSnack', mostPreferredSnack);
Qualtrics.SurveyEngine.setEmbeddedData('SecondMostPreferredSnack', secondMostPreferredSnack);
Qualtrics.SurveyEngine.setEmbeddedData('LeastPreferredSnack', leastPreferredSnack);
Qualtrics.SurveyEngine.setEmbeddedData('SecondLeastPreferredSnack', secondLeastPreferredSnack);
Qualtrics.SurveyEngine.setEmbeddedData('ThirdLeastPreferredSnack', thirdLeastPreferredSnack);

});

 


Thank you both! It seems to be working now. 


Hello @omkarkewat,

Thank you for your answer, it solved the same issue for me for ranking questions, much appreciated! I wanted to apply this to heatmap questions, but it seems to be a bit more complicated, do you have any suggestions?

Thank you in advance.


 Hi @DTDTDT Honestly I haven’t worked with heatmap question but just gave it a try after reading your query.

I learned that heatmap question has 2 values for 2 axes i.e.,  X and Y respectively. So you can set some branch logics and attach embedded data to it. I have created this as an example according to my understanding.

Now you can use pipe text to insert the ‘Quadrant’ embedded data value wherever required.


Hello @omkarkewat,

It worked, thank you so much!

DT


Leave a Reply