Hide an option of Multiple Choice Question | XM Community
Skip to main content
Solved

Hide an option of Multiple Choice Question

  • March 26, 2020
  • 21 replies
  • 1778 views

Hi, I'm new here. I've seen that people use jQuery("#"+this.questionId).hide() to call and hide a question but how can i hide an option of the question? Thanks in advance.

Best answer by TomG

In your code I think choice_contents[i] is an object, not a text field. Also, there is no guarantee that the choiceid is the same as the loop number + 1.
I would take a different approach (assumes single answer, vertical MC - not tested):
jQuery("#"+this.questionId+" label.SingleAnswer").each(function() {
var label = jQuery(this);
if(label.text() == "${e://Field/Name}") label.closest("li").hide();
});

21 replies

rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • March 26, 2020
If there is a specific condition for which the option need to be hidden then use choice display logic

  • Author
  • March 26, 2020

i can’t use display Logic because this question is conditioned by the answer of the previous question and both are in the same view. I tried to include the display logic but the option was not available.


Rudi
QPN Level 3 ●●●
Forum|alt.badge.img+16
  • QPN Level 3 ●●●
  • March 26, 2020
Hi Jorge, if I understand your requirement correctly you show two questions on the same page. Once the respondents select an answer in the first question some answer options in the second question should be hidden? I see one major "problem" in this setup: The respondent will always see all answer options when the page is loading. You might consider using custom validation here to prevent the "wrong" answer combination. Best regards, Rudi

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • March 26, 2020
> @JorgePs91 said: > Hi, > I'm new here. I've seen that people use jQuery("#"+this.questionId).hide() to call and hide a question but how can i hide an option of the question? > Thanks in advance. It depends on the format of the question and the criteria for hiding it. An example of hiding the choice where choiceid=1 in a vertical MC: ``` jQuery("#"+this.questionId+" input[choiceid=1]").closest("li").hide(); ```

  • Author
  • March 26, 2020
Hi @TomG In my case i'm using columns in MC, it seems like a table. How can i call the choice in row 1 and column 3? Thanks in advance

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • March 26, 2020
> @JorgePs91 said: > Hi @TomG > In my case i'm using columns in MC, it seems like a table. How can i call the choice in row 1 and column 3? > Thanks in advance I can't answer without knowing more and seeing the question. My advice is to use the inspect feature of your browser to figure it out.

  • January 8, 2021

https://www.qualtrics.com/community/discussion/comment/23503#Comment_23503Hello Tom,
Can this code be adapted to hide an option of the question, but using the naming of the option instead of the id?
For example: if the multiple choice question has several names, is it possible to write a code that hides the option "John"?
Best regards,
Francisco.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 8, 2021

  • January 8, 2021

https://www.qualtrics.com/community/discussion/comment/33200#Comment_33200Thank you for your answer, I was trying to do it through:
jQuery("#"+this.questionId+" input[Choice_____=${e://Field/Name}]").closest("li").hide()
but I just don't know how to find what I should write instead of "ChoiceID", in order to get the question to be hidden based on the name...
I also tried to find the choiceID of the name, and then apply the code you wrote above, but I can't get it to work...
var a = getChoicesFromVariableName("${e://Field/Name}");
var id=a[0];
jQuery("#"+this.questionId+" input[choiceid=" + id + "]").closest("li").hide();
Do you have any suggestions you're willing to share, please?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 8, 2021

https://www.qualtrics.com/community/discussion/comment/33202#Comment_33202You can't do it directly with a selector. You'll need to loop through the choices and examine the label text.


  • January 8, 2021

https://www.qualtrics.com/community/discussion/comment/33204#Comment_33204I understand, so basically I would need to do something like:
var qid = this.questionId;
var choice_contents = Qualtrics.SurveyEngine.QuestionInfo[qid].Choices;
var i;
for (i = 0; i < choice_contents.length; i++) {
if (choice_contents[i] == "${e://Field/Name}"){
var a = i+1;
 jQuery("#"+this.questionId+" input[choiceid=" + a + "]").closest("li").hide();
}
}
I tried this without success... unfortunately i'm completely unfamiliar with javascript syntax, could you give me any feedback please?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • January 8, 2021

In your code I think choice_contents[i] is an object, not a text field. Also, there is no guarantee that the choiceid is the same as the loop number + 1.
I would take a different approach (assumes single answer, vertical MC - not tested):
jQuery("#"+this.questionId+" label.SingleAnswer").each(function() {
var label = jQuery(this);
if(label.text() == "${e://Field/Name}") label.closest("li").hide();
});


  • January 11, 2021

https://www.qualtrics.com/community/discussion/comment/33209#Comment_33209Hello Tom,
I just tried it and it worked perfectly! Thank you very much for your help and patience!
Best regards,
Francisco Leandro.


  • March 18, 2021

Hello Tom,
I was wondering if you could help me. I am getting an error when I try to use the code - it is an unexpected end of input error. I am pasting the code below. I have a multiple choice drop down list where I would like to hide one of the choices that was already selected in a previous question. I don't want the user to select the same choice in both questions. I am not familiar at all with JavaScript, so I am hoping you could help me.

Qualtrics.SurveyEngine.addOnReady(function()
{
               /*Place your JavaScript here to run when the page is fully displayed*/
jQuery("#"+this.questionId+" label.SingleAnswer").each(function() {
   var label = jQuery(this);
   if(label.text() == "${q://QID9/ChoiceGroup/SelectedChoices}") label.closest("li").hide();
             
});      


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • March 19, 2021

I think it is just a syntax error. Try:
Qualtrics.SurveyEngine.addOnReady(function() {
jQuery("#"+this.questionId+" label.SingleAnswer").each(function() {
    var label = jQuery(this);
    if(label.text() == "${q://QID9/ChoiceGroup/SelectedChoices}") label.closest("li").hide();
});             
});    


  • March 23, 2021

That cleared the error. However, it is not doing what I expected. What I am trying to do is hide from the dropdown the value selected in question 9. Both questions are country lists and I would like to ensure that the user does not select the same country twice. I thought that the easiest way would be to hide the choice already selected. Do you have any suggestions how I could do this.
Thank you for the help.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • March 24, 2021

https://www.qualtrics.com/community/discussion/comment/35950#Comment_35950As I said above, the code is for a single answer, vertical MC question. A select (dropdown) is completely different. Also, you can't hide select options in all browsers; the best you can do is disable them.


Forum|alt.badge.img+1
  • August 15, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/33209#Comment_33209@TomG How would this be different if we assumed a dropdown list (single answer)?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 15, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/48493#Comment_48493The equivalent for a drop down would be something like (untested):
jQuery("#"+this.questionId+" select option").each(function() {
var option = jQuery(this);
if(option.text() == "${e://Field/Name}") option.prop("disabled",true).hide();
});


Forum|alt.badge.img+1
  • August 15, 2022

https://community.qualtrics.com/XMcommunity/discussion/comment/48502#Comment_48502TomG Hmm, I tried this and it is disabling each option but not hiding them for some reason. Very weird. Do you know why this would be?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • August 15, 2022

Yes, as I mentioned somewhere up above, not all browsers support hiding select options. Your browser must not.