Using keypresses for questions with choice randomization | XM Community
Skip to main content

Hello,

I currently have survey-takers pressing "E" to choose the left choice and "I" to choose the right choice. The question looks like this:
image.pngI want to keep it this way but randomize the choices. As is, "E" specifically triggers choice 1 and "I" choice 2.

Here is my current code:
let that = this;
Event.observe(document, 'keydown', function keydownCallback(e){

if (e.keyCode == 69) 
{
jQuery(this).trigger ("click");
that.setChoiceValueByRecodeValue(1,true);
that.clickNextButton();
}

if (e.keyCode == 73) 
{
jQuery(this).trigger ("click");
that.setChoiceValueByRecodeValue(2,true);
that.clickNextButton();
}
})
Is there any way I could get "1" and "2" to represent the left choice and the right choice?

I guess I might need to randomize the choices manually? Any ideas?

You can get the choice order like this:
var choices = jQuery("#"+this.questionId+" [choiceid]");
var leftChoice = choices.first().attr("choiceid");
var rightChoice = choices.last().attr("choiceid");
Then:
...
if (e.keyCode == 69) 
{
jQuery(this).trigger ("click");
that.setChoiceValue(leftChoice,true);
that.clickNextButton();
}
...


https://community.qualtrics.com/XMcommunity/discussion/comment/51922#Comment_51922This worked! I just had to change the variable name from choices for some reason. Thank you!


Previous message:

That seems like it would be perfect. For some reason it's not working.

When I console log the
rightChoice
and
leftChoice
, I get undefined.


Working from the beginning:

When I console log choices, I get an object that I don't really understand. It looks like it's a length of 1 when it's set to a variable, different from the jQuery somehow.

When I console log choices.first(), I get an object that I don' t understand. It looks like there are no elements/values, just keys.


image.png

Zoom in of the jQuery:

image.png

The query makes the left choice the zeroeth element, and the right choice the first element, so I could use that.

For some reason choices[0] and choices[1] return undefined. However, indexing the query works. E.g.

jQuery("#"+this.questionId+" [choiceid]")[0])
Returns:


The choice id is right there for the left option! I just need to reach it..


Leave a Reply