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

Limit Choices in Multiple Select

  • December 10, 2020
  • 6 replies
  • 366 views

karahansobrien
Level 1 ●
Forum|alt.badge.img+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!

Best answer by ahmedA

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);
                });
        }


    }
});

6 replies

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2050 replies
  • Answer
  • December 10, 2020

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);
                });
        }


    }
});


karahansobrien
Level 1 ●
Forum|alt.badge.img+7
  • Author
  • Level 1 ●
  • 11 replies
  • December 10, 2020

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


karahansobrien
Level 1 ●
Forum|alt.badge.img+7
  • Author
  • Level 1 ●
  • 11 replies
  • December 10, 2020

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


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2050 replies
  • December 11, 2020

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);
                });
        }


    }
   
});


Forum|alt.badge.img+5
  • Level 5 ●●●●●
  • 157 replies
  • April 2, 2024

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


Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • 2050 replies
  • April 2, 2024

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

Thanks.