Question with custom javascript code not displaying in results? | XM Community
Solved

Question with custom javascript code not displaying in results?

  • 4 June 2020
  • 5 replies
  • 219 views

We coded a question in Javascript for our survey. Previously we've used this question with no glitches (it's the same one copied from a previous project.) Now, it's not showing up in the results files for our new survey even though it's being displayed to participants. I called Qualtrics support, and they confirmed that it's nothing to do with the survey logic, and directed me here.
FWIW, I also checked the previous surveys that have used this question (ones where we've downloaded the results with no problem) and all of a sudden, they're not showing it, either. I'm wondering if Qualtrics or JS had an update where they're no longer compatible?
I'd really appreciate any advice on the matter. Thank you!

icon

Best answer by TomG 4 June 2020, 19:43

View original

5 replies

Userlevel 7
Badge +27

What type of question is the JS attached to and what is the JS code?

The type of question is text/graphic, and the code is below. Thanks!
function doll() {
  var canvas = document.getElementById("myCanvas");
   canvas.width = 900;
   canvas.height = 675;
  canvas.style.width = "900ptx";
  canvas.style.height = "675ptx";
  var ctx = canvas.getContext("2d");
  var img1 = new Image();
   img1.setAttribute('crossOrigin', '');
  img1.onload = function () {
  ctx.drawImage(img1, 0, 0);
  }
  img1.src = "https://upenn.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_6zM5WGjdWwZ0t2l";

  }
  
   /*board
   * function: Draws the board of available pins
   * input: int number of pins
   * output: none
   *
   */
  function board(pins) {
  var canvas = document.getElementById("myCanvas");
  var ctx = canvas.getContext("2d");
  ctx.fillStyle = "#ffffff";
  ctx.fillRect(600, 0, 270, 675);
  var xpin = 630;
  var ypin = 20;

  var img2 = new Image();
   img2.setAttribute('crossOrigin', '');
      img2.onload = function() {
      for (var i = 1; i <= pins; i++) {
      ctx.drawImage(img2, xpin - 30, ypin - 110, 90, 90);
      if (i == 10) {
      xpin = 702;
       ypin = 20;
       continue;
      }
      if (i == 20) {
      xpin = 774;
      ypin = 20;
      continue;
      }
      ypin+=67;
     if (!img2.complete) {
    console.log("Pin not loaded, coordinates were: " + xpin + ", " + ypin);
  } else {
         console.log("Pin has loaded, coordinates were: " + xpin + ", " + ypin);
          }
        }
      }
   img2.src="https://upenn.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_dgSGjhfzHFDr1xr";

    }

 function start() {
 doll();
  board(30);
  clicker();
 }
  

   /*mouselistener
   * function: "listens" for whenever the mouse is pressed.
   * when mouse is pressed, it calls upon the "addpin()" funct.
   * input: none
   * output: none
   *
   */
  function clicker () {
  var canvas = document.getElementById("myCanvas");
  var ctx = canvas.getContext("2d");
canvas.addEventListener("click", addpin, false);
 console.log("clicker runs!");
  }
  
  //body part variables
  var pins = 30;
  var head = 0;
  var neck = 0;
  var arms = 0;
  var hands = 0;
  var armpits = 0;
  var torso = 0;
  var groin = 0;
  var legs = 0;
  var feet = 0;
  
   /*addpin
   * function: when a part of the doll is clicked, draws a pin
   * where the mouse was clicked. Removes a pin from the board.
   * collects data on where on the doll the mouse was clicked,
   * based on the RGB of the area.
   * input: mouse event
   * output: none
   *
   */
  function addpin(event) {

  var img2 = new Image();
  img2.setAttribute('crossOrigin', '');
  var canvas = document.getElementById("myCanvas");
  var ctx = canvas.getContext("2d");
var rect = canvas.getBoundingClientRect();
  var x = event.clientX - rect.left;
  var y = event.clientY- rect.top;
  var pixeldata = ctx.getImageData(x, y, 1, 1).data;
   
  if(pins === 0) {
   return;
  }
  
  if (pixeldata[0] === 255 && pixeldata[1] === 255
  && pixeldata[2] === 255) {
  return;
  
  } else {
  window.pins--;
  board(window.pins);
  img2.onload = function() {
  ctx.drawImage(img2, x - 30, y - 70, 90, 90);
  }
     img2.src="https://upenn.co1.qualtrics.com/ControlPanel/Graphic.php?IM=IM_dgSGjhfzHFDr1xr";
  }
  //head- 253, 255, 255
  if (pixeldata[0] === 253 && pixeldata[1] === 255
  && pixeldata[2] === 255) {
  window.head++
  //window.alert("Head clicked");
  }
    //Neck- 255, 254, 255
  else if (pixeldata[0] === 255 && pixeldata[1] === 254
  && pixeldata[2] === 255) {
  window.neck++;
  //window.alert("Neck clicked");
  }
    //Arms- 255, 253, 255
  else if (pixeldata[0] === 255 && pixeldata[1] === 253
  && pixeldata[2] === 255) {
  window.arms++;
  //window.alert("Arms clicked");
  }
    //Hands- 255, 255, 254
  else if (pixeldata[0] === 255 && pixeldata[1] === 255
  && pixeldata[2] === 254) {
  window.hands++;
  //window.alert("Hands clicked");
  }
    //Armpits- 255, 255, 253
  else if (pixeldata[0] === 255 && pixeldata[1] === 255
  && pixeldata[2] === 253) {
  window.armpits++;
  //window.alert("Armpits clicked");
  }
    //Torso- 255, 254, 254
  else if (pixeldata[0] === 255 && pixeldata[1] === 254
  && pixeldata[2] === 254) {
  window.torso++;
  //window.alert("Torso clicked");
  }
    //Groin- 255, 254, 253
  else if (pixeldata[0] === 255 && pixeldata[1] === 254
  && pixeldata[2] === 253) {
  window.groin++;
  //window.alert("Groin clicked");
  }
    //Legs- 255, 253, 254
  else if (pixeldata[0] === 255 && pixeldata[1] === 253
  && pixeldata[2] === 254) {
  window.legs++;
  //window.alert("Legs clicked");
  }
    //Feet- 254, 255, 255
  else if (pixeldata[0] === 254 && pixeldata[1] === 255
  && pixeldata[2] === 255) {
  window.feet++;
  //window.alert("Feet clicked");
  }
  }

Qualtrics.SurveyEngine.addOnload(function()
{

start();

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

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

});

Userlevel 7
Badge +27

Your function never saves any data to Qualtrics. If it is attached to a descriptive text question, the only way to save any data to Qualtrics is with Qualtrics.SurveyEngine.setEmbeddedData(), and that isn't in your code.

Aah, I see, thank you for clarifying. It looks like when I copied the question from the previous survey, the code changed. The working task had the correct "Qualtrics.SurveyEngine.setEmbeddedData()"
The collaborator who coded it is not available right now, but I was wondering if there's any way at all to retrieve the data, as the question was displayed to participants. (I wouldn't think that there is, but I want to cover all of my bases.) Thanks!

Userlevel 7
Badge +27

The collaborator who coded it is not available right now, but I was wondering if there's any way at all to retrieve the data, as the question was displayed to participants.

Unfortunately not. The data was gone as soon as they left the page.

Leave a Reply