Java - displaying question with 'Space' keyboard entry for math problems | XM Community
Skip to main content

Dear community,
I am fairly new to using Java in Qualtrics. For one of our current experiments, we want to present participants with math problems, and have them write the answer in a text field. We want them to be able to display the correct answer by clicking 'Space', but still be able to write their own answer. We also want to record whether they clicked on 'Space' or not. I've looked for similar questions on the forum, and found some codes that seems to allow me to display the answer upon keyboard entry, specifically with 'Enter', but I have not been able to get it to work with 'Space'. I also don't know how to make sure that the keyboard entry is recorded as a response.
Would appreciate any help with this!
Best,
Simen

Qualtrics.SurveyEngine.addOnload(function() {
           jQuery("#"+this.questionId).hide();
});
 
Qualtrics.SurveyEngine.addOnReady(function() {
   document.on("keydown", function(e) {
       if (e.keyCode === 32) $("#"+this.questionId).show();
                       });
});

Hi there, if you still need, this can be done by first setting an Embedded Data Element at the top of the Survey Flow to capture if Space is clicked or not. Create a field called "Space" and set the value to "0". Then, on the Text/Graphic question that contains the correct answer, add the following JS:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

var that=this.questionId;
jQuery("#"+that+" .QuestionText").hide();

Event.observe(document, 'keydown', function keydownCallback(e){
if (e.keyCode == 32) 
{
Qualtrics.SurveyEngine.setEmbeddedData( 'Space', 1 );
jQuery("#"+that+" .QuestionText").show();
}
})

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Qualtrics.SurveyEngine.addOnPageSubmit(function(type)   
{
/*Place your JavaScript here to run when the page is unloaded*/

Event.stopObserving(document, 'keydown')

});


Thanks for the answer, Tom! We found a work-around, but it might be useful to pilot the original version with space and our modified version to see if we get identical answer. Cheers :)


Leave a Reply