Calling back a keyboard binding javascript function | XM Community
Skip to main content
Hello!

I am currently writing a survey where I want to use the spacebar to advance, using the following code:

Qualtrics.SurveyEngine.addOnload(function() {
$('NextButton').hide();
document.on("keydown", function(e) {
if (e.keyCode === 32) $('NextButton').click();
});
});

The issue is, once I enter it into one question, it applies for the rest of the questions too and becomes an issue when I need respondents to type in a response. If a respondent hits the space bar when typing out their response, they will advance to the end of the survey.

With that said, my question is: How do I unbind the space bar after the respondent is finished with the multiple choice section? I am not very well versed in JavaScript, and while I did attempt to write a callback script for the "Upon page exit" section, I couldn't get it to work.

Thanks!
I believe that in this instance .on is from prototypejs. If it is, you could assign it to a handler and then use .stop():
```
var handler = document.on("keydown", function(e) {
if (e.keyCode === 32) {
handler.stop();
$('NextButton').click();
});
```
I recommend using jQuery's .on and .off instead.

Leave a Reply