Join this month's Badge of the Month 💬 discussion about Qualtrics love to earn your exclusive badge! | Earn Badge >

Javascript Template Literal Placeholders Not Displayed in Question

edited May 2020 in Custom Code

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, 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

  • SurajK
    SurajK India Level 3 ●●●
    edited May 2020 Accepted 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,



    /*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];


    //var out = "Does the above image look "+term+" to you?";



    AND the below text should be in your question text,

    Does the above image look <span class="qntxt"></span> to you?