Autoselect based on previous response | XM Community
Skip to main content

Hello,

I am just getting started with JS and was hoping I could get some guidance on writing some script.

In this scenario, I have two questions, both have the same response options. The first is a multichoice format, and the second is singlechoice. I want to add some code to the second question, such that if only one option was selected at Q1, then that response will be autoselected at Q2 (e.g. Q1=’What brands of car are you considering’ & Q2=’What is your most preferred brand’. If they only have one brand in the initial list, then that has to be the answer to Q2 as well).

This is the current code that I have loading on Q2, but it doesn’t correctly select the response from Q1. Can anyone advise what the problem might be?

 

Qualtrics.SurveyEngine.addOnload(function() {
    var selectedChoices = "${q://QID1/ChoiceGroup/SelectedChoices}".split(",");
        if (selectedChoices.length === 1 && selectedChoicesp0] !== "") {
        this.setChoiceValueByRecodeValue(selectedChoicese0], true);
    }
});
 

Hi @cgillon I got this code from an another post which had a similar use case of yours, can you try the below code in Q2.

 

Qualtrics.SurveyEngine.addOnReady(function() {
    var qobj = this;
    var displayedChoices = C];
    jQuery.each(qobj.getChoices(), function(index, value) {
        if(qobj.getChoiceDisplayed(value)) displayedChoices.push(value);
    });    
    if(displayedChoices.length == 1) {
        qobj.setChoiceValue(displayedChoicesb0], true);
        qobj.clickNextButton();
    }
});


Thank you for the response on this, but that script didn’t seem to work for me.


Can you elaborate on how you are displaying the choices in Q2? You should carry forward the selected choices in Q1 to show it in Q2.

Also please make sure no other custom code is overriding this code.


In my final code, Q2 won’t be shown at all. I’ll hide Q2 on load, then check to see if only one option has been selected at Q1. If only one option has been selected at Q1, then that option should be automatically selected for Q2, then the script will auto-advance to the next question.

If more then one option has been selected at Q1, then those options are carried forward to Q2 and the respondent then selects one of those options as there #1.

I’ve kept things simple for this testing stage, so there isn’t any other coding at work.


Can you share screenshots of your setup?
Below is my setup.

Q1 is multi select question.
Q2 choices are carry forwarded from Q1 selected choices
JS Code used in Q2

This is the working link of my setup.


My apologies. I hadn’t understood what the code was doing, and thus hadn’t fully set up my test demo (I just had a crude version which didn’t yet have the ‘carry forward’ options installed). I had been thinking that the code would check the number of options selected at the previous question, but I now understand it is only checking the number of displayed options at the current questions, and thus the ‘carry forward’ step is key.

I have set things up properly now, and the script is also working perfectly for me.

Thank you for your help with this.


Leave a Reply