I have a question in a survey that uses the Essay Text Box type which allows users to enter multiple lines of text with line breaks. I need to run code to escape special characters, like line breaks, and then assign the escaped string to an embedded data field so that I can send that escaped text to another API via a POST request. These answer values must be escaped to be proper JSON in order to be properly received by the API.
I try something like this in embedded javascript:
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var myvar = "${e://Field/notmodified}";
var anothervar = JSON.stringify(myvar);
console.log(anothervar);
});
where the value of the embedded data field "notmodified" has been assigned in the survey flow using piped answer text:
"one fish
two fish"
The result is invalid javascript, as it appears that when I try to assign the piped text to "myvar", what happens is this:
var myvar = "one fish
two fish";
var anothervar = JSON.stringify(myvar);
This means that I can't run JSON.stringify on "myvar" because it is an invalid string. A template literal would work nicely here, but apparently we can't use those in embedded javascript.
Is there anything I can do to piped text to make it valid for use in javasccript?
Best answer by TomG
View original