How to run code on only some of the questions? | XM Community
Skip to main content

Hi everyone,
I found this code to advance by pressing "enter", and wrote it on specific questions.
But after the first time the subject press Enter to move forward, in all questions s.he can move forward by Enter. Even when I do not want the subject to be able to proceed in this way, and the code is not written there.
Maybe someone knows how to deal with it?
many thanks!

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();
                            }
             }
});

It stays active across pages because you attached it to the document. 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().


thank you vary much dear TomG !
Please let me know if I did what you intended:

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

Qualtrics.SurveyEngine.addOnUnload(function() {
document.removeEventListener('keydown',function);
}


https://www.qualtrics.com/community/discussion/comment/31379#Comment_31379Your syntax is wrong. Moreover, if you are using removeEventListener(), your addEventListener() function can't be anonymous. Even though the syntax is wrong, that seems like what you have tried to do.


Maybe someone can help me fix the code?
My JS knowledge is limited ...


Leave a Reply