Saving keypress as choice for each loop in multiple choice question

ehaynes00ehaynes00 Community Member Qubie ✭
edited October 16 in Custom Code

Saving keypress as choice for each loop in multiple choice question

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.

Answers

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

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

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

    Then use:

    qobj.setChoiceValue(1, true);
    
  • ehaynes00ehaynes00 Community Member Qubie ✭

    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.

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    My 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.

Sign In to Comment