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.
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();
});
Sign up
Already have an account? Login
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join.
No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login with Qualtrics
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join. No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login to the Community
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join.
No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Login with Qualtrics
Welcome! To join the Qualtrics Experience Community, log in with your existing Qualtrics credentials below.
Confirm your username, share a bit about yourself, Once your account has been approved by our admins then you're ready to explore and connect .
Free trial account? No problem. Log in with your trial credentials to join. No free trial account? No problem! Register here
Already a member? Hi and welcome back! We're glad you're here 🙂
You will see the Qualtrics login page briefly before being taken to the Experience Community
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
