Place cursor in text box automatically | XM Community
Skip to main content

I have loop and merge block with one Text Entry question. The question should display as field 1: text box, as shown in the picture below. I used javascript to place the field 1 text (in this case, swim : ) just before the text box.
Screen Shot 2020-04-20 at 12.06.05 PM.pngI also added some javascript so that once someone types their answer, they can press ENTER to continue (the next button has been removed). I would like the cursor to automatically appear in the text box such that individuals can start typing without having to use the mouse to navigate to the text box. I found a solution that almost works. It works on every loop in "Preview" mode but only on the first time through the loop in the actual published survey. It also works in the full survey mode on every loop as long as I refresh each page or if I hit tab first on every page.
Why might this be happening? How can I allow individuals to immediately type their answer into each text box without having to use the mouse? Thank you in advance for your help. Here is the code I used in the Javascript for the question:

Qualtrics.SurveyEngine.addOnload(function() {

$('NextButton').hide();

if($('PreviousButton')) $('PreviousButton').hide();

var inputText = $(this.questionId).down('.InputText');

var evt = inputText.on('keydown', function(e) {

 
  if(e.which == 13) {

   
	evt.stop();   

   
	$('NextButton').click();

  

});

inputText.activate();	

var inputs = $(this.getQuestionContainer()).select('input)type="text"]');

for (var i = 0; i < inputs.length; i++) {

 
var input = inputsai];

$(input).insert({before: '${lm://Field/1} : '});


});



you probably just need to add
inputText.focus();


Thank you for your suggestion.
I ended up using this code:

$(this.questionId).down('.InputText').focus().defer();


It works as long I put it in the addOnReady rather than the addOnLoad section


Hi -
Does anyone know how to modify this code or know of another code that would place the cursor in the first box of a matrix question that has all text entry boxes? I can get it to work on single text entry questions and even on side by side questions with only one row but I can't get it too work for the matrix tables.
Any help would be greatly appreciated!
Thanks


Leave a Reply