Answer via the keyboard in a multiple choice question with 2 options | XM Community
Skip to main content

Hi everybody :)
I created an experiment that tests working memory in which digits are presented quickly (a lot of them, 38 digits in 3 different blocks), and responding via the mouse is very slow for this.
I want to allow my subjects to respond using the keyboard with the letters "M" for an appropriate response, and "X" for an inappropriate response - currently the options appear horizontally with M being the response to the right answer.
After wandering around the various forums, I realized that I need to write code in JAVASCRIPT but I don't understand writing code at all.


I would appreciate help!

I came across this question in the forum - but I still can't find a way to adapt this code to me:

https://community.qualtrics.com/custom-code-12/use-keyboard-for-answering-multiple-choice-question-8398?postid=25171#post25171

 

 

Hi 🙂 Can you share how the question looks like and what is the desired outcome?

It will help providing you the correct code


Thank you very much for your willingness to help!


This is an N-BACK task. Subjects are presented with digits one after the other, and when a digit is presented, they need to see if the digit matches the one presented  2 digit before it. Each digit was created as a multiple-choice question with 2 options presented to them horizontally, one with a green circle emoji for the correct answer (on the right) and the other with a red circle for the incorrect answer (left) - I attach a picture for illustration. they also have timing Q for each digit.


Because I have digits, I am not sure what the correct way to use the code (should I enter a new code for each digit?)

 


Hi ​@Hamutal ,

You need to Add JavaScript to the question. 

Then, add the TomG’s code from that thread and amend the “J, K” keys to “M, X” for your case.

You can either swap the order of “M, X” to “X, M” or swap the number value at qobj.setChoiceValueByRecodeValue to change the order (left and right).


Thanks for the help!
I pasted the following code from Tom, but it doesn't work..

What do you think could be the reason?

 

Qualtrics.SurveyEngine.addOnload(function() {
    var qobj = this;
    jQuery(document).keypress(function(event) {
        var key = event.key.toUpperCase();
        if(key == "J" || key == "F") {
            jQuery(this).trigger("mouseup");
            if(key == "J") qobj.setChoiceValueByRecodeValue(1,true);
            else qobj.setChoiceValueByRecodeValue(2,true);
        }
    });
});


Correction, I'm sorry.
I changed the order at qobj.setChoiceValueByRecodeValue
So the code now looks like this, it still doesn't work..

 

Qualtrics.SurveyEngine.addOnload(function() {
    var qobj = this;
    jQuery(document).keypress(function(event) {
        var key = event.key.toUpperCase();
        if(key == "J" || key == "F") {
            jQuery(this).trigger("mouseup");
            if(key == "J") qobj.setChoiceValueByRecodeValue(1,left);
            else qobj.setChoiceValueByRecodeValue(2,right);
        }
    });
});


I will point out that I have tried all of these options that do not work.

setChoiceValueByRecodeValue:
qobj.setChoiceValueByRecodeValue(1,true);
qobj.setChoiceValueByRecodeValue(right,true);
qobj.setChoiceValueByRecodeValue("right",true)


Thanks for the help!
I pasted the following code from Tom, but it doesn't work..

What do you think could be the reason?

 

Qualtrics.SurveyEngine.addOnload(function() {
    var qobj = this;
    jQuery(document).keypress(function(event) {
        var key = event.key.toUpperCase();
        if(key == "J" || key == "F") {
            jQuery(this).trigger("mouseup");
            if(key == "J") qobj.setChoiceValueByRecodeValue(1,true);
            else qobj.setChoiceValueByRecodeValue(2,true);
        }
    });
});

 

Hi ​@Hamutal ,

You forgot the portion on changing the “J, F” to your “M, X”. 

You can either swap the order of “M, X” to “X, M” or swap the number value at qobj.setChoiceValueByRecodeValue to change the order (left and right).

And since the keys from the code were “J, K”, key J will select the left choice, and key K will select the right choice.

 

In your case, either swap the key from “M, X” to “X, M” or the number “1, 2” to “2, 1” to change the order.

 

Result for this scenario:

Qualtrics.SurveyEngine.addOnload(function() {
    var qobj = this;
    jQuery(document).keypress(function(event) {
        var key = event.key.toUpperCase();
        if(key == "X" || key == "M") {
            jQuery(this).trigger("mouseup");
            if(key == "X") qobj.setChoiceValueByRecodeValue(1,true);
            else qobj.setChoiceValueByRecodeValue(2,true);
        }
    });
});

 


Thank you very much for the detailed explanation!

But the code still doesn't work.. and I can't figure out why.. or what I'm doing wrong...
What do you think could be the problem?

 

 

Qualtrics.SurveyEngine.addOnload(function() {
    var qobj = this;
    jQuery(document).keypress(function(event) {
        var key = event.key.toUpperCase();
        if(key == "X" || key == "M") {
            jQuery(this).trigger("mouseup");
            if(key == "X") qobj.setChoiceValueByRecodeValue(1,true);
            else qobj.setChoiceValueByRecodeValue(2,true);
        }
    });
});


Hi ​@Hamutal,

  1. Check with your brand administrator if your account has permission to use JavaScript as not all accounts may have access to JavaScript. 
  2. Check if you have the recode the choices to 1 and 2. (Recode Values)
  3. Check if you paste the code correctly in Javascript section of the question. (Add JavaScript)

Fyi, i did test the code from the thread since it has been some time, and it is working.


Leave a Reply