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

Javascript not resetting between blocks

  • January 29, 2021
  • 3 replies
  • 41 views

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

Forum|alt.badge.img+22
  • Level 7 ●●●●●●●
  • January 29, 2021

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


rondev
Level 6 ●●●●●●
Forum|alt.badge.img+22
  • Level 6 ●●●●●●
  • January 30, 2021

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


  • Author
  • January 30, 2021

Thanks rondev, this worked perfectly.