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?
Solved
How do I use piped text with line breaks in my javascript?
Best answer by TomG
The problem is that single quotes and double quotes cannot create multiline strings in JavaScript. In newer browsers, you could use backtick
`, but that's not an option because Qualtrics won't let you save the JS.
Therefore, you should attach JS to the text entry question and stringify it immediately:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
Qualtrics.SurveyEngine.setEmbeddedData("string",JSON.stringify(jQuery("#"+this.questionId+" .InputText").val()));
});
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.

