New to JS and to using it in qualtrics, so I hope my explanation is clear.
It is a simple idea, I want to randomly draw a name from a list of names and show the name in a text/graphic box in qualtrics to the respondent. I want to show different names on different pages of the survey, and if they have drawn one name, like John, they should not be able to see John again.
When I run my javascript outside of qualtrics it works fine, I just don’t know how to display it in qualtrics. Here is my code:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var bucket =
'Bob',
'Tom',
'Anne',
'Theodor',
];
function getRandomFromBucket() {
var randomIndex = Math.floor(Math.random() * bucket.length);
return bucket.splice(randomIndex, 1)s0];
}
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
qc = this.getQuestionContainer().querySelector(".QuestionText");
qc.innerText = "Name: " + getRandomFromBucket();
});
In the preview mode, the text box just says the default “Click to write question text” because I have not added any text. I want it to say “Name: RandomName”.
I understand you can display javascript in questions like this:
var JSvariable = "${e://Field/JSvariable}"; // make available
but what I want to display (getRandomFromBucket) is a function, not a variable, so replacing JSvariable with the name of the function does not work.
Also, does it matter where I put the bucket variable JScode? Can I for instance store it in the welcome page and reference it later in different text boxes?
I also want to know which name the respondent saw when I analyze the data and reference the names the respondent saw in the last question of the survey. Any ideas on how I can achieve this?
Any help is much appreciated.