Javascript Template Literal Placeholders Not Displayed in Question

Hi community,
I'm very new to javascript and Qualtrics custom coding so my issue may be a simple one but it doesn't seem to have a solution presented anywhere. In my survey I implement some js code to randomize the use of a term across questions. For example:
// randomly select term
var term_array = ["big", "scary", "inviting", "attractive", "confusing", "opaque"];
var rnd = Math.floor(Math.random()*6);
var term = term_array[rnd];
var out = `Does the above image look ${term} to you?`;
The string in the variable "out
" is then outputted to an html script which displays the question.
The problem is that everything in the outputted string except the placeholder is being displayed when I preview the question.
So to illustrate, if term
= "scary", instead of displaying "Does the above image look scary to you?" the output displayed is "Does the above image look to you?"
This is unexpected because when I check the code in other javascript editors (such as https://js.do), the correct output is always displayed. Is there any Qualtrics-specific reason why this may be occurring? And is there another way to go about interpolating strings which may avoid this problem altogether? Thank you!
Best Answer
-
Try this way instead,
var out = 'Does the above image look "+term+" to you?';
And if you want to pipe the random string in the question text in Qualtrics then you can use the below code,
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var term_array = ["big", "scary", "inviting", "attractive", "confusing", "opaque"];
var rnd = Math.floor(Math.random()*6);
var term = term_array[rnd];
jQuery('.qntxt').html(term)
//var out = "Does the above image look "+term+" to you?";
//console.log(out)
});
AND the below text should be in your question text,
Does the above image look <span class="qntxt"></span> to you?
1
Answers
-
Thank you, this worked!
Any reason why template literals would not work?
0 -
$
is a reserved token for referring to internal data such as answers and embedded data. That's why even jQuery commands don't work with$
, but instead need to usejQuery
.0
Categories
- 8.7K All Categories
- 206 Community Information
- 549 CX Dashboards
- 6.9K Developer Corner
- 306 Employee Experience
- 7 Expert Knowledge
- 349 Stats iQ
- 2.3K Survey Platform
- 5K Survey Platform (Before March, 2021)
- 6 UX & UX Research
- 18 Virtual XM Events
- 328 Website / App Feedback
- 187 xFlow
- 206 XM Directory
- XM Discover