Restriction of command

I wrote a JS code for progress by press “Enter” (in Qualtrics):

Qualtrics.SurveyEngine.addOnload(function() {
    var qid = this.questionId;
    document.onkeydown = function(event) {
        console.log('keydown', event);
        if (event.which == 13) {
            event.preventDefault();
            jQuery('#NextButton').click();
        }
    }
});

But the code also affected parts of the survey that I was not interested that the participant can proceed in this way.

I got the following advice:

If you set up the event with addEventListener() or jQuery on() then you can remove the event in the addOnUnload() function with removeEventListener() or jQuery off().

but I don’t know how to fix the code in accordance. maybe someone can help me in fixing the code please?

I will be very grateful!

Answer

Do the following. The keydown event will only apply to the current survey page.

Qualtrics.SurveyEngine.addOnload(function() {
             jQuery(document).keydown(function(event) {
                            if (event.which == 13) {
                                          event.preventDefault();
                                          jQuery('#NextButton').click();
                            }
             });
             Qualtrics.SurveyEngine.addOnUnload(function() {
                            jQuery(document).off('keydown');
             });
});

.keydown() is a shortcut for .on(‘keydown’, function()… You don’t use qid, so you don’t need it. console.log is only for debugging so you don’t need that either.

Leave a Reply

Your email address will not be published. Required fields are marked *