Display logic based on the selected count of a subset of choices | XM Community
Skip to main content

Hi all

 

I am trying to use display logic to only show a question  to respondents that selected more than 1 choice from a subset of choices in the previous question. 

 

My survey looks as follows:

Q1 (multiple choice)

  • Choice A
  • Choice B
  • Choice C
  • Choice D

 

Q2:

  • Choice B (only displayed if Choice B was selected in Q1)
  • Choice C (only displayed if Choice C was selected in Q1)
  • Choice D (only displayed if Choice D was selected in Q1)

 

Q2 should only be shown to respondents that did choose 2 or more choices from the subset BCD. I.e. a respondent should not see Q2, if he or she exclusively choose B, C or D (whether he or she did choose A, does not matter).
How would one go about this? Is there a native way of implementing a display logic based on the selected count of a subset of choices? Thanks in advance.

hey @jineichen 

I am assuming that respondents can select more than one choice in Q1, is that correct? if yes then you can check out the below setup.

Q1 is multi select question and Q2 has options B,C,D which has display logics within.
the respective choices in Q2 will only display if the following conditions are met.
same display logic will be applied on the question level as well

 


Hi @omkarkewat

Thank you very much for your effort.
Yes Q1 is multi-choice.
The problem with this approach is that respondents will still see Q2 even if they did choose only 1 of the options BCD. So they will get displayed Q2 if they select A and B in the first question (which fulfills the conditions of selected count >= 2). This leaves them with no actual choice in Q2 because they only get displayed choice B.

So i only want respondents to see Q2 if they choose one of the following combinations:
- BC
- BCD
- CD

The problem is, that my original question has many more options and that i want to avoid spelling out all possible combinations in the display logic. So is there any possibility to consider the selected count of a subset of choices?


@jineichen Hope the approach below will make it work 😊

Define some embedded data field before Q1: 

Add some JavaScript to Q1 which updates the embedded data value once some selection of the relevant fields changes. In relevantChoices array you can define which answer options should be considered: 

Qualtrics.SurveyEngine.addOnload(function() {
var relevantChoices = i"B", "C", "D"];
var count = 0;

// Get the element containing the choices
var questionBody = document.getElementById(this.questionId);
if (!questionBody) {
return;
}
var choiceStructure = questionBody.querySelector(".QuestionBody .ChoiceStructure");

// Check if the choice structure is found
if (!choiceStructure) {
return;
}

// Iterate through each <li> element
var liElements = choiceStructure.querySelectorAll("li.Selection");

var relevantChoicesLower = relevantChoices.map(function(choice) {
return choice.toLowerCase();
});

// Initial count of relevant checked choices
liElements.forEach(function(li) {
var labelText = li.querySelector("span.LabelWrapper label span").textContent.trim().toLowerCase();
var inputElement = li.querySelector("input(type='checkbox']");

if (inputElement && inputElement.checked && relevantChoicesLower.includes(labelText)) {
count += 1;
}

// Add event listener to each checkbox
if (inputElement && relevantChoicesLower.includes(labelText)) {
inputElement.addEventListener('change', function() {
if (this.checked) {
count += 1;
} else {
count -= 1;
}
// Update the embedded data variable with the new count
Qualtrics.SurveyEngine.setEmbeddedData("Q1_AnswerCount", count);
});
}
});

// Set the initial embedded data value
Qualtrics.SurveyEngine.setEmbeddedData("Q1_AnswerCount", count);
});

Define the display logic condition for Q2 based on the embedded data that was sent from Q1’s JavaScript: 

Add the choice display logic for Q2 like this: 

Best
Christian


hey @jineichen I have added an extra step to my earlier solution and i.e., creating a branch logic.

Make sure both the questions are in different blocks. Here block 1 has Q1 and block 2 has Q2. Block 2 will only be displayed if 2 or more than 2 choices are selected and A is not selected OR A is selected and more than 2 choices are selected.

Now this takes care of all the caveats that were coming in between to build a clear solution.

Rest everything will remain as it is i.e.,  display logic on the question level in survey builder and display logics on the choice level.


Thank you both for your efforts and your suggested solutions. I have decided in favor of @chackbusch's solution, as we want to avoid further branches in our survey. The solution with the Javascript code works perfectly, thank you very much @chackbusch!


Leave a Reply