Option logic code for piping selected choices across multiple questions into a matrix table?
I'm fielding a survey that asks respondents about their various medical conditions. The conditions themselves are split across eight questions, one for each condition category, and look vaguely like this. There are 128 total conditions across the eight questions.
I’d like to do a follow-up matrix table question that pipes in all selected choices from across the eight questions, not including the “I do not have any of these conditions” exclusive statements. However, I don’t know the specific JS option logic code that would accomplish this. The code for piping in selected choices provides this; however, it still pipes in the exclusive statements.
${q://QID99/ChoiceGroup/SelectedChoices}
(I’m assuming eight rows of JS is what I’d need for the table rows: I know that carry forward can’t do this, nor will manually pulling in each condition and applying display logic, since I’d need to have >100 answer choices in the matrix table.)
Can this be done?
Page 1 / 1
Hi @cccpr1
Mabey this will work for you
Yes, you can achieve this using JavaScript to filter out the exclusive statements and pipe in only the selected choices from the eight questions. Here's how you can do it:
Qualtrics.SurveyEngine.addOnload(function() { // Get selected choices from each question var selectedChoices = e]; for (var i = 1; i <= 8; i++) { var qid = 'QID' + i; var selected = "${q://"+qid+"/ChoiceGroup/SelectedChoices}"; // Exclude "I do not have any of these conditions" selected = selected.replace("I do not have any of these conditions", ""); selectedChoices.push(selected.trim()); } // Combine selected choices into a single string var combinedChoices = selectedChoices.join(", "); // Pipe in combined choices to the matrix table jQuery("#"+this.questionId+" tbody tr td:eq(1)").text(combinedChoices);});
Here's a breakdown of what this script does:
It loops through each of the eight questions.
It retrieves the selected choices from each question using the ${q://} piped text.
It removes the exclusive statement "I do not have any of these conditions" from each set of selected choices.
It combines all selected choices into a single string.
It pipes in the combined choices into the matrix table.
You'll need to replace QID1, QID2, ..., QID8 with the actual question IDs of your eight medical condition questions, and you'll also need to adjust the matrix table selector if necessary.
Hi @RickB - Incredibly helpful, thanks so much! Think I’ve almost got it.
To confirm a few things re: the jQuery instruction for the piping:
Should I put the jQuery instruction in the JavaScript editor, the matrix table response, or both?
If I need to put it in the JS editor, do I need to repeat the instruction for each of my eight questions?
Do I need to replace the # and this.questionID variables with anything related to the eight questions?
Qualtrics.SurveyEngine.addOnload(function() { var selectedChoices = []; for (var i = 1; i <= 8; i++) {var qid = 'QID' + i; var selected = "${q://QID99/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID100/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID108/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID111/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID112/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID113/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID114/ChoiceGroup/SelectedChoices}"; var selected = "${q://QID115/ChoiceGroup/SelectedChoices}"; selected = selected.replace("I am not currently managing a cardiovascular health issue", ""); selected = selected.replace("I am not currently managing a women's health issue", ""); selected = selected.replace("I am not currently managing a respiratory health issue", ""); selected = selected.replace("I am not currently managing a form of arthritis", ""); selected = selected.replace("I am not currently managing a bone/joint condition", ""); selected = selected.replace("I am not currently managing a gastrointestinal condition", ""); selected = selected.replace("I am not currently managing a skin condition", ""); selected = selected.replace("No", ""); selected = selected.replace("Prefer not to say", ""); selectedChoices.push(selected.trim()); var combinedChoices = selectedChoices.join(", "); jQuery("#"+this.questionId+" tbody tr td:eq(1)").text(combinedChoices);}; Qualtrics.SurveyEngine.setEmbeddedData("combinedChoices" ,combinedChoices); });
@RickB Above is the code that I’m using for the follow-up question. (Note that one of the conditions is a single select, and each of the remaining seven has a slightly different exclusive response - hence the multiples at selected.replace.) It’s providing me with this:
I’m sure I’m doing something very basic incorrectly, but I’ve no idea what? I don’t generally get this advanced in JS.
Hi @cccpr1
Here's a breakdown to address your questions:
Placement of jQuery instruction: You should put the jQuery instruction in the JavaScript editor. You don't need to put it in the matrix table response because the JavaScript will handle the piping dynamically.
Repetition in the JavaScript editor: You only need to include the jQuery instruction once in the JavaScript editor. It will handle piping for all eight questions collectively.
Replacement of variables: You need to replace # with the appropriate selector for your matrix table, and you don't need to replace this.questionId because it dynamically refers to the current question being processed.
If you're using a standard matrix table question type, the # selector might look something like #QR~QIDXX where XX is the question ID of your matrix table question. If you have multiple matrix tables and want to target a specific one, ensure you're using the correct question ID.
So, in summary, make sure the jQuery instruction is placed in the JavaScript editor, replace # with the appropriate matrix table selector, and ensure you have the correct question ID for the matrix table if you're targeting a specific one.
ADMIN NOTE: This answer contains AI generated content that may or may not be accurate. For additional questions, contact @AlonsoC
I see what might be causing the issue. It seems like there's a misunderstanding regarding how the code is structured within the for loop. Each time you declare var selected = "${q://...}", you're overwriting the previous value of selected. Instead, you should accumulate the selected choices for each question within the loop. Here's the corrected version of your code:
Qualtrics.SurveyEngine.addOnload(function() { var selectedChoices = c]; for (var i = 1; i <= 8; i++) { var qid = 'QID' + i; var selected = "${q://QID" + qid + "/ChoiceGroup/SelectedChoices}"; // Replace exclusive statements for each question if (qid === "QID99") { selected = selected.replace("I am not currently managing a cardiovascular health issue", ""); } else if (qid === "QID100") { selected = selected.replace("I am not currently managing a women's health issue", ""); } else if (qid === "QID108") { selected = selected.replace("I am not currently managing a respiratory health issue", ""); } else if (qid === "QID111") { selected = selected.replace("I am not currently managing a form of arthritis", ""); } else if (qid === "QID112") { selected = selected.replace("I am not currently managing a bone/joint condition", ""); } else if (qid === "QID113") { selected = selected.replace("I am not currently managing a gastrointestinal condition", ""); } else if (qid === "QID114") { selected = selected.replace("I am not currently managing a skin condition", ""); } else if (qid === "QID115") { selected = selected.replace("No", ""); selected = selected.replace("Prefer not to say", ""); }
// Trim and push selected choices to the array selectedChoices.push(selected.trim()); } // Combine selected choices into a single string var combinedChoices = selectedChoices.join(", "); // Pipe in combined choices to the matrix table jQuery("#"+this.questionId+" tbody tr td:eq(1)").text(combinedChoices); // Store combined choices as embedded data Qualtrics.SurveyEngine.setEmbeddedData("combinedChoices", combinedChoices); });
In this corrected version, each selected choice is appended to the selectedChoices array within the loop, and exclusive statements are replaced conditionally based on the question ID. Finally, the combined choices are piped into the matrix table and stored as embedded data outside the loop.