Autoselect based on previous response | XM Community
Solved

Autoselect based on previous response

  • 3 March 2024
  • 6 replies
  • 54 views

Badge +3

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 && selectedChoices[0] !== "") {
        this.setChoiceValueByRecodeValue(selectedChoices[0], true);
    }
});
 

icon

Best answer by omkarkewat 3 March 2024, 09:59

View original

6 replies

Userlevel 5
Badge +12

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 = [];
    jQuery.each(qobj.getChoices(), function(index, value) {
        if(qobj.getChoiceDisplayed(value)) displayedChoices.push(value);
    });    
    if(displayedChoices.length == 1) {
        qobj.setChoiceValue(displayedChoices[0], true);
        qobj.clickNextButton();
    }
});

Badge +3

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

Userlevel 5
Badge +12

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.

Badge +3

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.

Userlevel 5
Badge +12

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.

Badge +3

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