How can I hide a questions that aren't selected from a multiple choice multiple answer question? | XM Community
Solved

How can I hide a questions that aren't selected from a multiple choice multiple answer question?

  • 26 September 2020
  • 9 replies
  • 78 views

Hi Everyone,
I'm constructing a survey and encountering a problem with hiding questions.
Quick run through of the situation: The first question asks respondents to select 1 or more characteristics of a product that can be improved. Each of the answers (if selected) has 3 additional questions that should be shown. For each answer not selected, those 3 additional questions belonging to the answer should be hidden and record the default choices.
After reading through many of the questions in the community, I think I need to hide questions using JavaScript so that the response will set as the default choice that it's set to.
I've tried using display logic and it will not keep the default choices because the question isn't displayed. I've tried using embedded data but I need the default choices as the response due to exporting purposes.
I attempted to use code similar to below, but it wouldn't work if multiple answers were selected. I would appreciate any help.
Qualtrics.SurveyEngine.addOnload(function() {
if("${q://QID1/SelectedChoicesRecode}"!="2") jQuery("#"+this.questionId).hide();
});
Thanks!

icon

Best answer by TomG 26 September 2020, 14:29

View original

9 replies

Userlevel 7
Badge +27

You need to hide the question then loop through the selected choices and show the question if "2" is selected.
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId).hide();
jQuery.each("${q://QID1/SelectedChoicesRecode}".split(", "),function(i,val){
if(val == "2") { q.show(); return; }
});
});

Hi TomG
Thanks for the help! So when using this code, the questions are shown for a short time and automatically advance. How can we make it so that the question won't advance until the next button is clicked by the respondent?

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/30682#Comment_30682The code I provided above doesn't advance to the next page. So, that must be coming from someplace else (auto advance, timer question or other JS).

https://www.qualtrics.com/community/discussion/comment/30689#Comment_30689Cool, it works now that I cleared everything and re-entered the code. Thank you!

TomG ,
Another question: how can I make it auto advance if the question is hidden?
I tried adding an else after, but don't think I'm doing it right.. (new at JS)
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId).hide();
  jQuery.each("${q://QID90/SelectedChoicesRecode}".split(", "),function(i,val){
  if(val == "1") { q.show(); return; }
else {this.clickNextButton();}
  });
});

Userlevel 7
Badge +27

'this' no longer refers to the question object inside the each(). Plus, you don't want to advance inside the loop. Try this:
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId).hide();
 jQuery.each("${q://QID90/SelectedChoicesRecode}".split(", "),function(i,val){
  if(val == "1") { q.show(); return; }
 });
if(q.is(":hidden")) this.clickNextButton();
});

This new code is auto advancing for both situations. :(

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/30743#Comment_30743Change q.not(":visible") to q.is(":hidden") as shown above.

https://www.qualtrics.com/community/discussion/comment/30744#Comment_30744Perfect, that works great! Thanks so much TomG !

Leave a Reply