JS: Auto-select Random Question and Hide from Participants | XM Community
Skip to main content

I've had some help from the forum, and I wanted to share my code. This allowed me to create a multiple choice question, auto select an answer, and hide the question's answers from the participants. (I just deleted the "question" field and put a space in so it would not display). NOTE: I had to select the choice to "Display answers in a random order" because this code does not include randomizing.
The magic of this code is that instead of endless survey flow with the same blocks repeated several times each with different words for one little part, I could have one set that I put in piped choices for. I hope this serves someone well!

This first bit of code allowed me to hide the answers. I did it this way because another way caused the question and answers to flash for a brief moment instead of never appearing:
Qualtrics.SurveyEngine.addOnload(function () {
  this.getChoiceContainer().hide();
});

This next bit is what chose the answer for the participants automatically:
Qualtrics.SurveyEngine.addOnReady(function () {
  var $this = jQuery(this.questionContainer);
  $this.hide();
var $choices = jQuery(".QuestionBody .ChoiceStructure .LabelWrapper label for]", $this);
  var randomChoice = Math.floor(Math.random() * $choices.length + 1) ;
  jQuery(".QuestionBody .ChoiceStructure .LabelWrapper labelpfor]:eq(" + randomChoice + ")", $this).click();
});

(The reason for all of that was because display logic causes the question to just not occur. I needed it to occur but not be seen.)


Leave a Reply