JavaScript is triggered inconsistently | XM Community
Skip to main content

I use JavaScript to load a list of ten labels from embedded data, randomly extract one, display it, and overwrite the embedded data with the remaining labels. The participants write down text for each label which is then also stored in embedded data. I present this block ten times so that all the labels are displayed. When I test the survey this seems to work perfectly, yet when I have real participants take it, it sometimes doesn't seem to work. This is apparent by the fact that for some participants the list of labels (which should be empty at the end of the survey) has some remaining labels in it and there's no answers for these labels. It seems like the JavaScript fails to be executed in some blocks for a majority of the participants. I unfortunately cannot find the mistake because it never appears for me. Does anyone have an idea what might be causing this?
Here's my code:
Qualtrics.SurveyEngine.addOnload(function()
{
// fetch answers from last trial's text entry fields
var ans_one = "${q://QID36/ChoiceTextEntryValue}";
var ans_two = "${q://QID37/ChoiceTextEntryValue}";
var ans_three = "${q://QID38/ChoiceTextEntryValue}";
var current_label = "${e://Field/current_label}";
var answers = [ans_one, ans_two, ans_three].join("|"); // concatenate answers
var used_label = "${e://Field/used_label}";
used_label = used_label + "," + current_label;
Qualtrics.SurveyEngine.setEmbeddedData(current_label, answers); // save answers
Qualtrics.SurveyEngine.setEmbeddedData('used_label', used_label); // save used labels

// Load label list
var label = "${e://Field/label}";
label = label.split(",");
var i = Math.floor(Math.random() * label.length);

  // Randomly select label and remove it from the label list
 var current_label = label.splice(i, 1);
label = label.join(",");

// Save labels
Qualtrics.SurveyEngine.setEmbeddedData('label',label);
Qualtrics.SurveyEngine.setEmbeddedData('current_label', current_label);

  // Insert label into the question
  document.querySelector("#label").innerText = current_label;

// load backup labels and skipped labels
var backup_label = "${e://Field/backup_label}";
var skipped_label = "${e://Field/skipped_label}";

// Select backup label if participant doesn't know label
this.questionclick = function(event,element){
if (element.type == 'checkbox') {
// append skipped label
skipped_label = skipped_label + "," + current_label;

// Randomly choose backup label
backup_label = backup_label.split(",");
i = Math.floor(Math.random() * backup_label.length);

current_label = backup_label.splice(i, 1);
backup_label = backup_label.join(",");

document.querySelector("#label").innerText = current_label;

// Reset skip button for next trial
jQuery("#QID43 input").prop("checked", false);
}

Qualtrics.SurveyEngine.setEmbeddedData('skipped_label',skipped_label);
Qualtrics.SurveyEngine.setEmbeddedData('current_label',current_label);
Qualtrics.SurveyEngine.setEmbeddedData('backup_label',backup_label);
};
// Reset skip button for next trial
this.setChoiceValue(4,false);

});

Instead of JS you can use simple single punch question to display '1' of them via randomization and then carry forward choices which are not displayed to the next one and follow the same process until the last one (i.e. 10th question), this will work without JS and will save you lots of time and efforts.

Let me know if this helps!!


Leave a Reply