Problem: Javacode carries over to next block | XM Community
Question

Problem: Javacode carries over to next block


Userlevel 1
Badge +2

Hi,
I have a javacode in one block that enables participants to answer by pressing on a key (L or Q) on their keyboard which automatically advances to the next block.
Problem, on the next block with an open ended question, when participants type in a text that includes one the key L or Q, it skips to the following question, like the java code is carrying over to the next block (while it isn't present in the code of this block). It doesn't happen when I put the block before the block with java.
Could you please help me with this!!! ?
Thank you very much


2 replies

Userlevel 7
Badge +21

Most probably the code isn't scoped properly.
Try putting everything between the Qualtrics.SurveyEngine.addOnReady({.....Put Everything Here..... })
If this doesn't work, then it's difficult to say anything without looking at the code.

Userlevel 1
Badge +2

Thank you for your answer ahmedA
Here is the full code .
It's the part where it advances when you press on L or Q that seems to carry over.

Qualtrics.SurveyEngine.addOnload(function()
{




var qid = this.questionId;
//load all images at once
var images = jQuery('img');
  var counter = images.length; 

images.each(function(){

jQuery(this).parents().eq(0).hide()

});

jQuery(function() {
function imageLoaded() {

     counter--; 
     if( counter === 0 ) {
  jQuery(this).parents().eq(0).show()
    }
   }
  
images.each(function() {

   if( this.complete ) {

imageLoaded.call( this );
   }
else {
     
jQuery(this).on('load', imageLoaded);
    }
   });
});



//Change background-color of the label
jQuery("#"+ qid+ "-1-label").css({"background-color":"white"})
jQuery("#"+ qid+ "-2-label").css({"background-color":"white"})


document.onkeydown = function(event) {
console.log('keydown',event);

if (event.which == 65 && counter === 0) {
event.preventDefault();

var rank = "${e://Field/question_rank}"
rank++
Qualtrics.SurveyEngine.setEmbeddedData('question_rank', rank);

Qualtrics.SurveyEngine.registry[qid].setChoiceValue(1, true);
jQuery('#NextButton').click();
} else if (event.which == 76 && counter === 0) {
event.preventDefault();

var rank = "${e://Field/question_rank}"
rank++
Qualtrics.SurveyEngine.setEmbeddedData('question_rank', rank);

Qualtrics.SurveyEngine.registry[qid].setChoiceValue(2, true);
jQuery('#NextButton').click();
}
}
});

Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery(".Skin .SkinInner").css({"min-width": "1220px"});
jQuery("#"+this.questionId+" label.SingleAnswer").css({"padding":"5px"});

jQuery('#NextButton').css("visibility","hidden");


//////Code pour sauvegarder l'ordre du loop and merge
var order = "${e://Field/loop_order}"
var new_order = order + "${lm://Field/3}" + ";"
Qualtrics.SurveyEngine.setEmbeddedData('loop_order', new_order);
/////////

});

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


});

Leave a Reply