Saving keypress as choice for each loop in multiple choice question | XM Community
Question

Saving keypress as choice for each loop in multiple choice question

  • 16 October 2020
  • 3 replies
  • 25 views

Hello!
I am programming a Navon task in Qualtrics and have the images set up as a loop & merge question with multiple choices (h and l). Functionally, my code is working to allow the keypress to click the next button and move on, but it's not saving the data from keypress anywhere in my data file and it also doesn't seem to be saving the keypress as a choice value for each loop and merge. Since it's technically allowing me to use the keypress, I'm assuming the lack of data saved has to do with the fact that it's loop & merge. Here's the code I'm currently using (but I have tried others as well):
Qualtrics.SurveyEngine.addOnload(function()
{
this.hideNextButton();
this.hidePreviousButton();
this.hideChoices();
var qid = this.questionId;

document.onkeydown = function(event) {
console.log('keydown',event);
if (event.which == 72) {
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();
}
}

});

If anyone has tips on what I might be doing wrong, I'd appreciate it.


3 replies

Userlevel 7
Badge +27

I'm not sure if this is the problem, but:
replace:

var qid = this.questionId;
 with:
qobj = this;

Then use:
qobj.setChoiceValue(1, true);

Thanks TomG for your response! Unfortunately, that doesn't seem to be the problem as that actually didn't let me even use the keypress to continue forward.

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/31213#Comment_31213My next guess is that your keycodes are incorrect. .which is case sensitive and 72 and 76 are uppercase, so you probably didn't use the Shift key. However, .which is deprecated so you should use .key instead, and the side benefit is you can use actual characters instead of keycodes.

Leave a Reply