Javascript not resetting between blocks | XM Community
Skip to main content

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.

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


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


Thanks rondev, this worked perfectly.


Leave a Reply