Controlling just one single question with keyboard, while disabling keyboard for all other questions | XM Community
Skip to main content
Solved

Controlling just one single question with keyboard, while disabling keyboard for all other questions

  • November 25, 2021
  • 2 replies
  • 22 views

I control a single MC question (yes/no) with arrow keys (this is working so far):
Qualtrics.SurveyEngine.addOnload(function() {
var qid = this.questionId;
jQuery(document).keydown(function(event) {
console.log('keydown', event);
if (event.key == "ArrowLeft") {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValueByRecodeValue(1, true);
jQuery('#NextButton').click();
} else if (event.key == "ArrowRight") {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValueByRecodeValue(0, true);
jQuery('#NextButton').click();
}
});
});
This script is affecting all questions (not only the one in which I put the code), but I want to limit the key bindings to one specific question only. How can I remove the binding?

Best answer by TomG

You need to turn off the keydown event handler after the left or right arrow keys are pressed using jQuery .off().

2 replies

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

You need to turn off the keydown event handler after the left or right arrow keys are pressed using jQuery .off().


  • Author
  • November 27, 2021

Thank you so much, it worked! For more information see https://api.jquery.com/off/