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

How to run code on only some of the questions?

  • October 22, 2020
  • 4 replies
  • 14 views

Forum|alt.badge.img

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

Best answer by TomG

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

4 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Answer
  • October 22, 2020

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


Forum|alt.badge.img
  • Author
  • October 23, 2020

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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • October 23, 2020

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.


Forum|alt.badge.img
  • Author
  • October 23, 2020

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