JavaScript for keyboard use with multiple choice question | XM Community
Skip to main content
Hi!



I've tried to come up with the right script for allowing participants to respond to a 2-answer multiple choice question by using the keyboard. I would like to have participants use the keyboard 'J' for answer 'No' and the keyboard 'F' for answer 'Yes'. I've recoded the values of the answers to the question as Yes = 1 and No = 2 (also see attachment image:

!



However it still doesn't work. This is what the script looks like now:



Qualtrics.SurveyEngine.addOnload(function()

{

console.log("test");

jQuery(document).keypress(function(event) {

if(event.which==74){Qualtrics.SurveyEngine.setChoiceValueByRecodeValue(2,true)

}

if(event.which==70){Qualtrics.SurveyEngine.setChoiceValueByRecodeValue(1,true)

}

});

});



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

});



Qualtrics.SurveyEngine.addOnUnload(function()

{

/*Place your JavaScript here to run when the page is unloaded*/

});



Is this script correct? It seems as if the script is not getting 'started'. Would be really happy to find a solution! Thank you, Paul

Qualtrics.SurveyEngine.addOnload(function()
{
console.log("test 4");
this.questionContainer.addEventListener('keydown',function(event) {
console.log("someone typed something");
if (event.isComposing || event.keyCode === 74) {
console.log("no");
}
if (event.isComposing || event.keyCode === 70) {
console.log("yes");
}
});
});


Hi! Sorry for the late response, but thank you very much for your solution. It's highly appreciated!


Leave a Reply