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

Randomly select N non-selected options from previous questions and create a piped text


Forum|alt.badge.img+3

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 = nonSelectedIndustries[i];
        nonSelectedIndustries[i] = 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", selectedIndustries[0]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", selectedIndustries[1]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", selectedIndustries[2]);
    Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", selectedIndustries[3]);
});

```

  • 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?

Best answer by dl2860

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 = ["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];
            nonSelectedIndustries[i] = 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(selectedIndustries[0]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[1]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[2]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[3]));

        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry1", selectedIndustries[0]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", selectedIndustries[1]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", selectedIndustries[2]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", selectedIndustries[3]);
    });

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

View original

3 replies

Forum|alt.badge.img+20
  • QPN Level 5 ●●●●●
  • 290 replies
  • August 7, 2024

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!


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 4 replies
  • August 7, 2024

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 = unselected[i];
Qualtrics.SurveyEngine.setEmbeddedData( "random_choice", unselected[i] );

});

```


Forum|alt.badge.img+3
  • Author
  • Level 1 ●
  • 4 replies
  • Answer
  • August 7, 2024

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 = ["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];
            nonSelectedIndustries[i] = 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(selectedIndustries[0]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[1]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[2]));
        Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", Qualtrics.SurveyEngine.getEmbeddedData(selectedIndustries[3]));

        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry1", selectedIndustries[0]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry2", selectedIndustries[1]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry3", selectedIndustries[2]);
        //Qualtrics.SurveyEngine.setEmbeddedData("SelectedIndustry4", selectedIndustries[3]);
    });

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


Leave a Reply