requirement is that I need to remove any new line characters from multiline text entry. I am using this answer in webservice (in workflow) and it is failing.
I think the best place to do this is is workflow and add code task right before webservice call (or just some javascript that changes the answer with stripped text). but i have no idea how to do that. the example in qualtrics code task does not make sense to me as I am new to javascript and javascript in qualtrics.
below is my JSON Free text and I need to replace any new lines in ${q://QID7/ChoiceTextEntryValue}
I am new to javascript and javascript in Qualtrics.
Best answer by chackbusch
Hi @pbedekar,
sorry, forgot that the behavior of the multi line and essay text entries is a little bit different. You should achieve it by adding a listener to the text area. Add some JavaScript to your question (independent of the actual question ID):
Qualtrics.SurveyEngine.addOnReady(function() { var qid = this.questionId;
// Find the textarea element of current's question var textarea = document.querySelector("#" + qid + " textarea");
if (textarea) { // Add an event listener to detect changes in the textarea textarea.addEventListener('input', function() { // Get the current value of the textarea var newValue = textarea.value;
// Replace new lines with spaces var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");
// Save the value to an embedded data field Qualtrics.SurveyEngine.setEmbeddedData("MY_TEXTAREA", newValue); }); } });
Before the value is stored, new lines are replaced by spaces. I added 3 lines:
This log shows you an example of each execution of the custom code:
This stores the current value of the text area in some embedded data field MY_TEXTAREA. The embedded data should be defined in the flow before your question.
You can use this in your webservice call body. The single lines are just concatenated with space:
This should avoid the error.
Hope this answers your question! If so, please mark my answer as solution. :-)
thanks for the code. but the code errors at assignment var originalValue = "${q://QID7/ChoiceTextEntryValue}" when there is new line in text. when I have one one line in QID7 answer it works fine.
sorry, forgot that the behavior of the multi line and essay text entries is a little bit different. You should achieve it by adding a listener to the text area. Add some JavaScript to your question (independent of the actual question ID):
Qualtrics.SurveyEngine.addOnReady(function() { var qid = this.questionId;
// Find the textarea element of current's question var textarea = document.querySelector("#" + qid + " textarea");
if (textarea) { // Add an event listener to detect changes in the textarea textarea.addEventListener('input', function() { // Get the current value of the textarea var newValue = textarea.value;
// Replace new lines with spaces var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");
// Save the value to an embedded data field Qualtrics.SurveyEngine.setEmbeddedData("MY_TEXTAREA", newValue); }); } });
Before the value is stored, new lines are replaced by spaces. I added 3 lines:
This log shows you an example of each execution of the custom code:
This stores the current value of the text area in some embedded data field MY_TEXTAREA. The embedded data should be defined in the flow before your question.
You can use this in your webservice call body. The single lines are just concatenated with space:
This should avoid the error.
Hope this answers your question! If so, please mark my answer as solution. :-)
Thanks @chackbusch. I feel I am very close. with you code I can see the multile getting tranformed in one like with spaces. but the Embeded data is not being set. had to make a little change to get textarea to work.. i did put the window.alert right before setembededdata and newvalue is as expected but the embeded data is blank.
Qualtrics.SurveyEngine.addOnReady(function() { var qid = this.questionId; // Find the textarea element of current's question var textarea = document.querySelector("textarea");
if (textarea) { // Add an event listener to detect changes in the textarea if (!textarea.dataset.listenerAdded) { textarea.addEventListener('input', function() { // Get the current value of the textarea var newValue = textarea.value;
// Replace new lines with spaces var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " "); // Save the value to an embedded data field Qualtrics.SurveyEngine.setEmbeddedData("Cleantext", newValue); }); } } });
finally. I replaced setembededData with setJSEmbeded data and now it works. Thank you very very much @chackbusch
Qualtrics.SurveyEngine.addOnReady(function() { var qid = this.questionId; // Find the textarea element of current's question var textarea = document.querySelector("textarea");
if (textarea) { // Add an event listener to detect changes in the textarea if (!textarea.dataset.listenerAdded) { textarea.addEventListener('input', function() { // Get the current value of the textarea var newValue = textarea.value;
// Replace new lines with spaces var newValue = newValue.replace(/(\r\n|\n|\r)/gm, ". "); // Save the value to an embedded data field Qualtrics.SurveyEngine.setJSEmbeddedData("Cleantext", newValue); }); } } });
sorry, forgot that the behavior of the multi line and essay text entries is a little bit different. You should achieve it by adding a listener to the text area. Add some JavaScript to your question (independent of the actual question ID):
Qualtrics.SurveyEngine.addOnReady(function() { var qid = this.questionId;
// Find the textarea element of current's question var textarea = document.querySelector("#" + qid + " textarea");
if (textarea) { // Add an event listener to detect changes in the textarea textarea.addEventListener('input', function() { // Get the current value of the textarea var newValue = textarea.value;
// Replace new lines with spaces var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");
// Save the value to an embedded data field Qualtrics.SurveyEngine.setEmbeddedData("MY_TEXTAREA", newValue); }); } });
Before the value is stored, new lines are replaced by spaces. I added 3 lines:
This log shows you an example of each execution of the custom code:
This stores the current value of the text area in some embedded data field MY_TEXTAREA. The embedded data should be defined in the flow before your question.
You can use this in your webservice call body. The single lines are just concatenated with space:
This should avoid the error.
Hope this answers your question! If so, please mark my answer as solution. :-)