How do you make jQuery logic not to carry over to the other questions? | XM Community
Skip to main content
In qualtrics, we have a set of questions which a participant has to answer and each question has a unique answer. If the participant answers it correctly, the background color will change blue otherwise it will turn red. It seems like the logic for the answer "cat" carries over to the following questions. How can we fix our code?



var bannedWords = ["cat"],

regex = new RegExp('\\\\b' + bannedWords.join("\\\\b|\\\\b") + '\\\\b', 'i');

console.log(regex);

var that = this;

Event.observe(document,'keydown',function(e){

var choiceID = null;

if (e.keyCode ==32) // if space was pressed

{

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

// alert(inputs);

jQuery(function () {

jQuery("input").on("change", function () {

var valid = regex.test(this.value);

if (valid)

{

jQuery("body").css("background-color","blue");

choiceID = 1;

var timer = setTimeout(explode, 4000);



}else{



jQuery("body").css("background-color","red");

choiceID = 1;

window.setTimeout(explode, 4000);

//alert("red");



}

//choiceID = 1;



}); //input.on functin



});

} //end of if statement



}); //event.observe



/*Timer expired function*/

var explode = function(){

if (choiceID==1){

alert(" explode2");

jQuery("body").css("background-color","white");

that.setChoiceValue(choiceID,false);

}



};



}); //end of section
Your problem is the reference to "body" which is the whole page, as in:

```

jQuery("body").css("background-color","red");

```

You should be referencing just the question, as in:

```

jQuery("#"+this.questionId);

```

Leave a Reply