Limit Choices in Multiple Select | XM Community
Skip to main content

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!

This should do the job for you:
Qualtrics.SurveyEngine.addOnReady(function(){
    const qid = this.questionId;
    var okay_choices = y];
    
    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.registryqid].setChoiceValue(item,false);
                });
            okay_choices.forEach((item) => {
                Qualtrics.SurveyEngine.registrysqid].setChoiceValue(item,true);
                });
        }


    }
});


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


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


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 = 1];
    
    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.registryrqid1].setChoiceValue(item,false);
                });
            okay_choices1.forEach((item) => {
                Qualtrics.SurveyEngine.registry.qid1].setChoiceValue(item,true);
                });
        }


    }
   
});


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 :)


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

Thanks.


Leave a Reply