Incoporating JavaScript to Qualtrics-how to limit valid responses to only two keyboard buttons? | XM Community
Skip to main content

Hi, I want to ask participant to answer my question by pressing 1 or 2 on their keyboard (which I am able to achieve with the script below), and I want to limit their response to only those two buttons (which I am struggling with). Particularly, they will not be able to proceed unless they press 1 or 2. For example, if they press 5, nothing will happen, and they will not be able to move on to the next question.
I am very new to incorporate Javascript in Qualtrics, and I am struggling with this issue for past two weeks. I really really hope this forum could help me with this issue, any advices would be appreciated. Many thanks! Please let me know if I should provide more information.
--------------
Updates:
Hi, I just found out that this script does not work when I press 2, like when I press 1, the question will proceed, but when I press 2, the proceed button will show up and quickly flash away, and I will still be stuck on this question. Therefore, the question only allows me to press 1, but I should be able to proceed when I press either 1 or 2.
Below is the script:
Qualtrics.SurveyEngine.addOnload(function()
{

this.hideNextButton();
this.hidePreviousButton();
var that = this;
Event.observe(document, 'keydown', function keydownCallback(e) {
 var choiceID = null;
 switch (e.keyCode) {
  case 49: // '1' was pressed
   choiceID = 1;
   break;
  case 50: // '2' was pressed
   choiceID = 2;
   break;
 }

 if (choiceID) {
  Event.stopObserving(document, 'keydown', keydownCallback);
  that.setChoiceValue(choiceID, true);
  that.clickNextButton();
 }
});
});

Move the line var choiceId = null before the Event.Observe line.


Hi, I just found out that this script does not work when I press 2, like when I press 1, the question will proceed, but when I press 2, the proceed button will show up and quickly flash away, and I will still be stuck on this question. Therefore, the question only allows me to press 1, but I should be able to proceed when I press either 1 or 2.
Thank you so much!


That shouldn't happen. If you can share the question, I maybe able to see what's going on.


Leave a Reply