Randomly select N non-selected options from previous questions and create a piped text | XM Community
Skip to main content

I have a question that asks people for the industry they work in (19 options total from a dropdown). In the next question, I want to randomly choose four of the non-selected options from this previous question to separately ask questions about each of the three non-selected choices. 

So far, I’m trying to use javascript and embedded data to create a piped text for these non-selected choices. My steps are:

 

  • In survey flow, create embedded data fields for each of the industry options: “ind1”, “ind2”, …. I set all of them to 0. 

 

  • In survey flow, replace embedded data fields with 1 for selected choice.
    1. After the Block containing the industry selection question, I create 19  Branch element where, inside each Branch, I set each industry embedded data field to 1 if a respondent selected that industry.

  • Add JavaScript to Randomly Select Non-Selected Options
    • I added the following JavaScript to randomly select three non-selected industries:

```

Qualtrics.SurveyEngine.addOnload(function() {
    // Define the industry options
    var industries = >"ind1", "ind2", "ind3", "ind4", "ind5", "ind6", "ind7", "ind8", "ind9", "ind10",
                      "ind11", "ind12", "ind13", "ind14", "ind15", "ind16", "ind17", "ind18", "ind19"];
    
    // Get the embedded data values
    var nonSelectedIndustries = industries.filter(function(industry) {
        return "${e://Field/" + industry + "}" == "0";
    });
    
    // Shuffle the non-selected industries
    for (var i = nonSelectedIndustries.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = nonSelectedIndustriesni];
        nonSelectedIndustries i] = nonSelectedIndustriesej];
        nonSelectedIndustriessj] = temp;
    }
    
    // Select the first three non-selected industries
    var selectedIndustries = nonSelectedIndustries.slice(0, 4);
    
    // Save the selected industries as embedded data
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry1", selectedIndustriest0]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", selectedIndustries01]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", selectedIndustriess2]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", selectedIndustriesi3]);
});

```

  • Use Piped Text for Follow-Up Questions: Use piped text to insert the randomly selected non-selected industries. For example:

 

However, the piped text $e://Field/SelectedIndustry1 is not showing up. What am I doing wrong?

Hey @dl2860  this can be done without Javascript as well. Use loop and merge.

 

STEP 1: Create a MCQ having 19 options.

I have created only 6 choices just for your reference. You can increase it upto 19.

 

STEP 2: Create a new block which will contain question(s) asked for non-selected options. 

Create a loop and merge setup which will be based on industry question only for non-selected choices. And at the bottom you can enter the number of times the question will be asked for a non-selected industry (which will be random and unique everytime). If I am not wrong, in your case it would ‘4’.

 

STEP 3: Finally pipe text the loop and merge field in the required question text.

END!


Thanks @omkarkewat for the response!

 

I am trying to use javascript because I have a follow up question that also uses the unselected choices in a matrix form:
 

 

 

The loop and merge definitely takes care of the multiple choice, but I can use it for the above matrix question. 

 

I should also note that my MCQ options includes comma, so I couldn’t use something like below (from this post) to separate out the unselected choices:

```

Qualtrics.SurveyEngine.addOnload(function()
{
var unselected = "${q://QID1/ChoiceGroup/UnselectedChoices}".split(", ");;
var i = Math.floor(Math.random() * unselected.length);
document.getElementById("demo").innerHTML = unselectedui];
Qualtrics.SurveyEngine.setEmbeddedData( "random_choice", unselectedui] );

});

```


For those with similar issues, I figured out how to do this with some tweaks to my original approach:
 

  1. Set embedded data: for each of the industry options (with actual values) from the MC question as well as the 4 placeholders for the randomly selected non-selected choices: 

     

  2. In survey flow, replace embedded data fields with 1 for selected choice. (same as above) 

     

  3. add the following javascript to the question after the MC question:
    ```

    Qualtrics.SurveyEngine.addOnload(function() {
        // Define the industry options
        var industries = s"ind1", "ind2", "ind3", "ind4", "ind5", "ind6", "ind7", "ind8", "ind9", "ind10",
                          "ind11", "ind12", "ind13", "ind14", "ind15", "ind16", "ind17", "ind18", "ind19"];
        
        // Get the embedded data values
        var nonSelectedIndustries = industries.filter(function(industry) {
            //return console.log("${e://Field/" + industry + "}") !== "1";
            return Qualtrics.SurveyEngine.getEmbeddedData(industry) !== "1";
        });
        
        // Shuffle the non-selected industries
        for (var i = nonSelectedIndustries.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = nonSelectedIndustries(i];
            nonSelectedIndustriesoi] = nonSelectedIndustries j];
            nonSelectedIndustries j] = temp;
        }
        
        // Select the first three non-selected industries
        var selectedIndustries = nonSelectedIndustries.slice(0, 4);
        
        // Save the selected industries as embedded data
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry1", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustriese0]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustriesd1]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustriesb2]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustriesE3]));

        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry1", selectedIndustries/0]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", selectedIndustries)1]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", selectedIndustries12]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", selectedIndustriess3]);
    });

  4. Use Piped Text for Follow-Up Questions (same as above)


Leave a Reply