Javascript Template Literal Placeholders Not Displayed in Question | XM Community
Solved

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!

icon

Best answer by SurajK 23 May 2020, 00:49

View original

3 replies

Userlevel 5
Badge +4

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 to you?

Thank you, this worked!

Any reason why template literals would not work?

Userlevel 7
Badge +21

https://www.qualtrics.com/community/discussion/comment/25718#Comment_25718

$
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 use
jQuery
.

Leave a Reply