Same JS code can't be run twice on Qualtrics survey | XM Community
Solved

Same JS code can't be run twice on Qualtrics survey

  • 21 November 2018
  • 4 replies
  • 10 views

Hi, I have a survey to be run that contains a dice roll task. The task is to be done twice by the same person, i.e., click a button to "roll" a dice then selecting an option to report the value that appears on the dice pic. The exact same JS code is used in two blocks of questions, and the two blocks are randomized. I previewed the whole thing for several times. The first task worked out well, but the second time, clicking the button just did not work and no number was shown on the dice pic. I pasted the question content and the JS code here, which are identical for both questions in which the task appears. The funny thing is, when I tried to next on the second task without selecting a value (which of course did not let me to because reporting a value was made "required") then reclick the button, the roll worked! I have no idea.

!


Qualtrics.SurveyEngine.addOnload(function()
{
var container_div = document.getElementById("container");
var dice_button = document.getElementById("dice_button");
var dice_container_div = document.getElementById("dice_container");

function randomIntFromInterval(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function roll_dice(dice_button, container_div, dice_container_div){
dice_button.onclick = function(){
var outcome = randomIntFromInterval(1,6);
var dice_pic_id = "dice_" + String(outcome) + ".jpeg";
var dice_link = "https://www.sas.upenn.edu/~jdorsett/experiment_pics/hannah/" + dice_pic_id;
var dice_pic = document.createElement("img");
dice_button.setAttribute("disabled", "disabled");
dice_pic.setAttribute('src', dice_link)
dice_pic.setAttribute('height', '300px');
dice_pic.setAttribute('width', '300px');
dice_container_div.appendChild(dice_pic);
container_div.style.display = 'none';

}
}
roll_dice(dice_button, container_div, dice_container_div);
});
icon

Best answer by RDurant 5 December 2018, 17:06

View original

4 replies

So I don't know why it does it. I assume the container or another variable is being stored somewhere and not updated, but if you still need an answer the easiest way i found to fix the problem was just name them differently (Containter2, dice_button2, dice_container2)
Thank you for the help. I had tried just that before I posted this question, but it did not work at that time. As I looked at it later, I recognized the last few lines being omitted during the renaming. So yeah, I was like half way to the solution but, sadly and stupidly, forgot to extend the change to some last few subjects.
It drives me crazy when I'm so close to an answer and something silly like that just mucks it all up. Just curious, why the down vote? Is it because I didn't know why the issues was occurring the first place.
Absolutely not! Must have been a mistake or something. I never intended to downvote your reply 🙂

Leave a Reply