Javascript to pre-select answers not working with matrix table | XM Community
Skip to main content

Hi all -

I’m using javascript to preselect answer options based on embedded data in multiple choice questions. @ahmedA gave me great tips on using const choices when I was having some issues there. Now I am struggling with how to set it up for matrix questions. See the question and code below - nothing is pre-selecting. Any advice very appreciated!

    const choices = this.getChoices();
    if ("${e://Field/ESGFirm}".includes("Yes")) this.setChoiceValue(0,0,true);
    if ("${e://Field/ESGFirm}".includes("No")) this.setChoiceValue(0,1,true);
    if ("${e://Field/ESGPortfolio}".includes("Yes")) this.setChoiceValue(1,0,true);
    if ("${e://Field/ESGPortfolio}".includes("No")) this.setChoiceValue(1,1,true);

 

 

You are getting the choices, but not using them anywhere in setting the answers.

For matrix, first get the choices. const choices = this.getChoices();

Then get the answers const answers = this.getAnswers();

Then set the answers with this.setChoiceValue(choicesc0],answerse0],true);

And so on.

 

That being said, I would strongly recommend you look at the support pages for pre-populating responses. That will be much easier and faster for you.


Thanks @ahmedA  - so would it be like this?
    const choices = this.getChoices();const answers = this.getAnswers();
    if ("${e://Field/ESGFirm}".includes("Yes")) this.setChoiceValue(choices(0],answers,0],true);
    if ("${e://Field/ESGFirm}".includes("No")) this.setChoiceValue(choicesl0],answers[1],true);
    if ("${e://Field/ESGPortfolio}".includes("Yes")) this.setChoiceValue(choicese1],answersc0],true);
    if ("${e://Field/ESGPortfolio}".includes("No")) this.setChoiceValue(choiceso1],answersh1],true);

Also, according to all reading I’ve done and convos with Qualtrics support themselves, they don’t offer this level of functionality? You can preset written answer options or a constant multiple choice/matrix selection, but if you want it to vary based on embedded data, my research and what I’ve been told is that I have to use JavaScript - if there is another option, would be great though!


Leave a Reply