How to run code on only some of the questions? | XM Community
Solved

How to run code on only some of the questions?

  • 22 October 2020
  • 4 replies
  • 3 views

Userlevel 2
Badge

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

icon

Best answer by TomG 22 October 2020, 22:28

View original

4 replies

Userlevel 7
Badge +27

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().

Userlevel 2
Badge

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

Userlevel 7
Badge +27

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.

Userlevel 2
Badge

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

Leave a Reply