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?
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()));
});
Thanks TomG. That is more or less what I ended up doing. I appreciate you taking the time.
Here's the ultimate problem with that though:
In order to use this embedded data field anywhere else the javascript code, I have to wrap it in another set of quotations which makes it more or less useless.
I tried wrapping it in single quotes the next time I needed to reference this embedded data field on the next page, but that would break any time there was an apostrophe in the string.
My only option is to figure out ways around using these embedded data fields in the code, which means that I have to build the payload that I'm sending to my API by hand outside of the code in one of the "Actions" or survey flow API transactions.
Way less than ideal.
https://www.qualtrics.com/community/discussion/comment/30578#Comment_30578HI TomG when you say it won't allow a backtick, has this recently changed as it seems possible to do. Having read this page: https://www.digitalocean.com/community/tutorials/how-to-work-with-strings-in-javascript
for example
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
alert(`he said, "I'm called r", loudly`);
});
seems to work as expected when you preview the question. If you try encasing it in single or double quotes qualtrics won't let you save it.
However I must admit I've not been too successful when trying to use the string from the URL line as the browser converts any double quotes to " but save the above string into a variable and then utilising it still seems to work. So I am assuming the an embedded field brought into the survey via a contact list method (rather than using the URL method) should work (but I guess I need to test this!)
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
//var test = '${e://Field/sts}'; //inputting a string via the URL route causes " to change to " so no good
var test = `he said, "I'm called r", loudly`; //inputting the text here work well so I imagine an embedded field thgouth a contact list should work
//alert(`he said, "I'm called r", loudly`); //this works well
//alert (JSON.stringify(test)); //this literally puts double quote either side and escapes any special characters like ' or a line return I think
alert (test);
});
Hope I understand correctly the above problem - I have so much to learn still after all!
Thanks
Rod Pestell
https://www.qualtrics.com/community/discussion/comment/36774#Comment_36774I guess Qualtrics has made an update because you couldn't use backticks back in Sept 2020. I haven't had the need to do anything like this recently.
Backticks work. But you still can't use the full power of template literals, as the editor won't allow $ inside them. This is probably to avoid conflicts with piped text.
https://www.qualtrics.com/community/discussion/comment/36779#Comment_36779I see, ok. I've yet to try out a true embedded field from a contact list which contains a ' and " in but think as soon as I bring it into javascript I will see that it converts " to " and the likes. So I think there will still be a problem!
FYI at the moment, I'm simply trying to replace a word in a phrase that contains a ' and " in (eg, the replace word apple with orange). It's very annoying this issue. I'm now going down the route of trying to figure out if I can replace it in situ through jquery on the webpage rather than saving it as a javascript variable first. So far drawn a blank as only found css amending and injecting new divs to add on a word on the end of something.
Any thoughts most welcome.
Thanks
Rod Pestell
I haven't seen the escaping happen. See this.
It uses regex:
.replace(/\\".+\\"|\\'.+\\'/gm, '"oranges"')
The line breaks are lost, but that's just a matter of mapping it to some unique characters like ~~~ or something.
@ahmedA,
Thanks for the test survey.... Apologies I wanted to replace apple with oranges without the need to put it in quotes. It's just that in the test string, in some cases it may contain single or double quotes elsewhere.
How do you use regex without using javascript or are you using Javascript? Will this still work with an embedded field rather than just a text box?
Thanks
Rod
Text manipulations are not possible in Qualtrics, you'll need to do them through JS. I've suggested it as a product idea, perhaps you can vote/comment on it, since it hasn't gained much traction.
Using JS, it will also work for embedded data.
Hi All,
Just wanting to link this conversation with another thread as it's talking about the same thing, which I'm still trying to work on - essentially resolving / escapting character returns (\\n) in a JSON string using javascript in an code task in an action. The problem being is that the string is injected into the javascript as a string (or so it looks like in the report) and so when there is a \\n it needs to be escaped. Catch22 basically - I need to get the string into the code in the first place but if it contains character returns it errors out in the first place.
Here's the link with my post at the bottom: https://community.qualtrics.com/XMcommunity/discussion/comment/41912#Comment_41912
Would be grateful for some advice on this.
Thanks
Rod Pestell
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.