Rolling a die in Qualtrics: die is not showing! | XM Community
Skip to main content

Hello,
I am trying to let participants roll a die for ten times (ten different dices), but I cannot see the dice...
This is my code in HTML:


 



 




 


 

and this is my code in javascript added to the question:

Qualtrics.SurveyEngine.addOnload(function()

  {var self = this;
var value_set = e

  {key:"1",probability:1},

  {key:"2",probability:1},

  {key:"3",probability:1},

  {key:"4",probability:1},

  {key:"5",probability:1},

  {key:"6",probability:1}

  ];

  var images = {

  base_url:'https://www.descil.ethz.ch/projects/1305-SensQuest/dice/img/',

  ext:'.png',

  init: 'qmark1.png'

  };

  // Instantiate dice 1

  new Dice("dice1","throw1",{

  possible_throws: 1,

  value_set: value_set,

  images: images,

  result_callback: function(throw_nr,result_key){

// Save result to embedded data field (need
  //not be set through survey flow settings)
  Qualtrics.SurveyEngine.setEmbeddedData('result_matrix_embedded', result_key);

// Set choice by result key, choice index respectively
  // Attention: choices must be in correct order (ie choice 1 must have value 1, etc)
  self.setChoiceValue('1', result_key, true)
  }
  });

});

What am I doing wrong? I can't figure it out...

I have also created an embedded data at the beginning of the survey flow


Hi there, I found a thread that I think will be helpful to you:
https://community.qualtrics.com/XMcommunity/discussion/3370/not-able-to-use-javascript-successfully
In that, PraDeepKotian_Ugam provides a QSF that has the dice animation working. I imported that file and adjusted the Survey Flow to add the Embedded Data field so that the dice roll value gets saved in the data. I've attached my updated QSF here.
Also, I found an interesting dice roll implementation that randomizes the answers of a Multiple Choice question and makes each of the answers an animated GIF with each possible (numerical) outcome of a roll of two dice.
https://pavlov.tech/2015/07/15/the-effects-of-age-on-the-windfall-bias/
RollingDice.qsf


Tom_1842 Thank you! Unfortunately, I cannot open the file... Can you also send the code here?
Thanks in advance,

Best,
Britt


Hi Britt, QSF files are only meant to be imported into Qualtrics. I'd recommend checking out how to import QSF files in case you come across QSF files in the future that you might find useful as the below code will already be in place.
https://www.qualtrics.com/support/survey-platform/survey-module/survey-tools/import-and-export-surveys/#ImportingASurvey
In the meantime, below is how to set up your survey:
1) Add an Embedded Data field to the top of your Survey Flow called "result_matrix_embedded1"
2) Create a Descriptive Text/Graphic Question type in the Survey Builder
3) In the HTML View of the question, input the below code:









 








4) The JavaScript is too many characters to post here, so I've included all of it in the attached .TXT file.
QualtricsDiceRollTXT.txt


thank you! Tom_1842


Tom_1842 I have one last question (it works perfectly now, thank you again!). I have multiple blocks in the survey of which one of them is to roll the dice 10 times (and report the outcome 10 times). However, does it matter for the rest of the survey blocks if the embedded data is at the beginning of the whole survey in the survey flow? Or does this not influence the other blocks?


Hi Britt, I am glad that the dice question is working for you! The "result_matrix_embedded1" through "result_matrix_embedded10" fields should be set at the top of your survey flow so that the fields exist when the Javascript in the dice question sets the value once the die is rolled.
Towards the bottom of the Javascript, there are parameters that can be played with such as the number of possible throws and if the 'throw dice' button is visible or not. I've adjusted the code to make the possible number of throws 10 and so that the button stops displaying once the possible throws is equal to the done throws. I've also adjusted the survey flow to include 10 of the "result_matrix_embedded" fields, 1 for each throw. The attached QSF should be all set.
RollingDice_10Throw.qsf


Thank you Tom_1842 !


Thank you!


Tom_1842
Hey! I used your code for throwing the dice 10 times and it works, thank you! However, I have to let them roll the die and then report the result (so they should not be allowed to throw it multiple times in one specific round of the experiment), and then they have to roll it in round 2 again and report the result, etc. So I have to make sure that they can only throw it once in every round and that I can see the result, but I have to let them do this 10 times. How can I adjust your code to get this?
Also, I get a warning when I want to save the javascript. It says: "Use of document.write in JavaScript is not allowed." Is this a problem when I want to distribute the survey?

Thank you in advance, you are really helping me out!


Hi Britt,
To have respondents roll a single die, proceed to another round, and then roll another single die, we can return to the code that was in the first QSF as that was mostly already configured that way.
First, Embedded Data placeholders can be included at the top of the survey flow, 1 for each throw. "1result_matrix_embedded1", "2result_matrix_embedded1", "3result_matrix_embedded1", and so on.
Using that Throw 1 question as the base, it can be copied 10 times and the code modified slightly so that each throw is self contained. In each questions' HTML, the code would change from "throw1" and "dice1" to "throw2"/"dice2", "throw3"/"dice3", and so on. 
Within each question's JavaScript, change "throw1" and "dice1" to "throw2"/"dice2", "throw3"/"dice3", and so on. Also change the line that sets the Embedded Data field to "1result_matrix_embedded", "2result_matrix_embedded", "3result_matrix_embedded", and so on.
This is all in place in the attached QSF.
As for the warning message, there is a line in the JS (497) that uses document.write, but it is commented out so it is not actually being run when respondents see the question. I think it's fine to leave in there, but lines 495-499 could also be removed from the JS and the warning message will go away.
RollingDice_1Throw_x10.qsf


Thank you Tom_1842 !! Now I am finally ready to distribute my survey :)


Leave a Reply