Javascript Template Literal Placeholders Not Displayed in Question

aoshotse
aoshotse CA Qubie ✭
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 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!

Tagged:

Best Answer

  • SurajK
    SurajK India Sage ✭✭✭
    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,

    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?

Answers