Limit Choices in Multiple Select | XM Community
Solved

Limit Choices in Multiple Select

  • 10 December 2020
  • 6 replies
  • 240 views

Userlevel 1
Badge +7

I am wondering if there is a way to limit the number of choices in a multiple select question. For instance, let's say I want the validation range to be Must Select "1" but No More than "3" and what I want to do (but can't figure out how) is if they have selected 3, I don't want them even be able to highlight another bar (or make another choice) unless they deselect one of the 3 already selected choices.
Case: I have a one page survey that has 10 questions and they won't get the error message until they try to hit the submit button, in which case I'm worried they won't scroll back up to see the error message and will instead close the browser.
Thanks in advance for advice!

icon

Best answer by ahmedA 10 December 2020, 21:08

View original

6 replies

Userlevel 7
Badge +21

This should do the job for you:
Qualtrics.SurveyEngine.addOnReady(function(){
    const qid = this.questionId;
    var okay_choices = [];
    
    that = this;
    this.questionclick = function(){
        sel_choices = that.getSelectedChoices();
        // Set the maximum number of choices you want here
        if(sel_choices.length <=3){
            okay_choices = sel_choices;
        }
        else{
            alert("You can not select more than 3 choices.\\nPlease deselect to select another.");
            sel_choices.forEach((item) => {
                Qualtrics.SurveyEngine.registry[qid].setChoiceValue(item,false);
                });
            okay_choices.forEach((item) => {
                Qualtrics.SurveyEngine.registry[qid].setChoiceValue(item,true);
                });
        }


    }
});

Userlevel 1
Badge +7

This is EXACTLY what I was looking for. Thank you!

Userlevel 1
Badge +7

Will this javascript not work for 2 questions in a row? Here is what I have in for Q6 and Q7 but it is only working for Q7.
Screen Shot 2020-12-10 at 4.23.42 PM.png

Userlevel 7
Badge +21

Too eager to celebrate were we. :-)
It appears that the variables are being shared across the questions and hence this will only work for the last question in which code is inserted. To make it work for all, you'll need to rename the variables (

qid
,
that
,
okay_choices
) for each question. You can change them to anything like (
qid1
,
qid2
,
qid3 
etc.) Just make sure, they are unique for each question. Here's how it should look finally:
Qualtrics.SurveyEngine.addOnReady(function(){
    const qid1 = this.questionId;
    var okay_choices1 = [];
    
    that1 = this;
    this.questionclick = function(){
        sel_choices = that1.getSelectedChoices();
        // Set the maximum number of choices you want here
        if(sel_choices.length <=3){
            okay_choices1 = sel_choices;
        }
        else{
            alert("You can not select more than 3 choices.\\nPlease deselect to select another.");
            sel_choices.forEach((item) => {
                Qualtrics.SurveyEngine.registry[qid1].setChoiceValue(item,false);
                });
            okay_choices1.forEach((item) => {
                Qualtrics.SurveyEngine.registry[qid1].setChoiceValue(item,true);
                });
        }


    }
   
});

Userlevel 6
Badge +5

Hi @ahmedA 

This worked well.

But do we have any further options to improve this?

Ideally removing the vanity URL and ‘says’.

 

Or some nicer looking Qualtrics warning message?

Thanks :)

Userlevel 7
Badge +21

Customizations will be chargable, drop me a PM if interested.

Thanks.

Leave a Reply