In the attached photo, I have a matrix question. I’m looking for some JavaScript so that a future question will only appear if at least 2 shows are selected. It does not matter if the boxes selected are computer, mobile, or tablet. Regular Qualtrics display logic gets massive. Qualtrics support suggested I ask the community about a JavaScript option.
Solved
Display logic from a Matrix question, using Javascript

Best answer by vaneskaw
Here’s the solution: Put this javascript into the Matrix question on the survey. Then go to the survey flow, and create an embedded field data, called “hasTwoOrMoreProducts” = Yes. Make sure this embedded field is on the top of the survey flow. Then for the questions where you only want the question to show if 2 or more products are selected, select the display logic on that question, then select “hasTwoOrMoreProducts” = yes. It works.
Qualtrics.SurveyEngine.addOnload(function() {
const questionId = this.questionId
// function to check matrix selection
function checkMatrixSelection() {
const matrix = jQuery("#"+ questionId +" table.ChoiceStructure tbody tr")
var uniqueProductCount = 0;
// loop through each row of the matrix to count unique product selections
matrix.each(function() {
const selected = jQuery(this).find('input:checked');
if (selected.length >= 1) {
uniqueProductCount++;
}
});
// Show or hide Question 2 based on unique product count
if (uniqueProductCount >= 2) {
console.log('more than 2')
Qualtrics.SurveyEngine.setEmbeddedData('hasTwoOrMoreProducts', 'Yes')
} else {
console.log('less than 2')
Qualtrics.SurveyEngine.setEmbeddedData('hasTwoOrMoreProducts', 'No')
}
}
// Add event listener to all matrix inputs
jQuery(this.getQuestionContainer()).on('change', 'input[type=checkbox]', function() {
checkMatrixSelection();
});
// Initial check in case the user navigates back
checkMatrixSelection();
});
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.