Javascript not resetting between blocks | XM Community
Question

Javascript not resetting between blocks

  • 29 January 2021
  • 3 replies
  • 31 views

Userlevel 1

Hi,

I am currently using the below code to allow key-presses that separate words into positive/negative categories and then moves onto the next word within the block.
The block after this asks the participants to input into a text box as many of the previous words as they can. From my testing, when I type "a" or "l" it leads to the "next" button being pressed. How do I stop the JS code from the previous block applying in the subsequent one?

Qualtrics.SurveyEngine.addOnReady(function(){
var qid = this.questionId;
$('NextButton').hide();

document.onkeydown = function(event) {
console.log('keydown',event);
if (event.which == 65) {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValue(1, true);
jQuery('#NextButton').click();
} else if (event.which == 76) {
event.preventDefault();
Qualtrics.SurveyEngine.registry[qid].setChoiceValue(2, true);
jQuery('#NextButton').click();
}
}


Thanks,
Chris.


3 replies

Userlevel 7
Badge +21

Define it as an observer and stop observing on next button click.

Userlevel 7
Badge +22

Paste the below code in the addOnUnload funtion of each question where you have the above code:
document.onkeydown = function(event) {};

Userlevel 1

Thanks rondev, this worked perfectly.

Leave a Reply