Defining embedded fields from Selected Choices of same question using JavaScript | XM Community
Skip to main content

I'm asking a "check all that apply" question (Question 1) and want to use items on the list of Selected Choices (e.g., the first item on the list) in a subsequent question (Question 2).
I would think that there are two approaches:
1) use JavaScript in Question 1 to define the embedded fields for use in Question 2; or
2) use a function in the text for Question 2 that refers back to the Selected Choices in Question 1 to get the values.
I'm indifferent as to which approach I use - just one that works.
When I tried approach 1, using the following JavaScript:
Qualtrics.SurveyEngine.addOnPageSubmit(function ()
{
var choicesall = this.getSelectedChoices();
let choicesarray = choicesall.split(",");
  Qualtrics.SurveyEngine.setEmbeddedData('origstring', choicesall);
    Qualtrics.SurveyEngine.setEmbeddedData('numofchoices', choicesarray.length);

for (i = 0; i < choicesarray.length; i++) {
var selectedchoice = choicesarraysi]
var embfieldname = "choicenumber" + i;
    Qualtrics.SurveyEngine.setEmbeddedData(embfieldname, selectedchoice);
}
});
the embedded variables, such as origstring, numofchoices, etc. are blank.

When I try approach 2, I know that I can use a construct like ${q://QID1/ChoiceGroup/SelectedChoices}. I get the full list of selected choices correctly. But is there a modification to the function call that pulls out item i from the list?
Any suggestion would be most appreciated.
Thanks!

I feel that the simplest approach would be to use, carry forward choices and then hide some of them in Q2. This will be you JS for Q2:
// Collect all choices displayed in Q2
let opts = this.questionContainer.querySelectorAll(".q-radio")
opts[0].parentElement.hide(); //Hide first choice
opts[1].parentElement.hide(); //Hide second choice
Using parentElement gives it the flexibility to work on both horizontal and vertical question types.
This is assuming you are piping them into a MC question.


Leave a Reply