Search code examples
javascriptqualtrics

JavaScript variable to piped text


I have a custom script that generates text output. I want to pipe this text into an embedded data field that then appears as piped text in the same question. I've set a blank embedded data field (let's call it myField), and included ${e://Field/myField} in the question text. The script does pipe into the ultimate data, but not into the question text.

The code displayed below uses code found on stack overflow to generate a similar output, etc.:

Qualtrics.SurveyEngine.addOnload(function()
{
    function getRandomSubarray(arr, size) {
    var shuffled = arr.slice(0), i = arr.length, temp, index;
    while (i--) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i];
        shuffled[i] = temp;
    }
    return shuffled.slice(0, size);
}

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var fiveRandomMembers = getRandomSubarray(x, 5);
Qualtrics.SurveyEngine.setEmbeddedData('myField', fiveRandomNumbers);
});

Am I missing a reference to this specific question?


Solution

  • Piped variables get resolved on the Qualtrics server before sending the page to the browser. If you want to dynamically update text on the page you have to do it with JavaScript, not piping. So put a span with an id where you have your piped text field in the question.

    <span id="fiveRandomMembers"></span>
    

    Then update the innerHTML of the span at the end of your script.

    jQuery("#fiveRandomMembers").html(fiveRandomMembers);