Passing data containing "ENTER (new line)" from one survey to another | XM Community
Skip to main content

I have two surveys: Survey 1 and Survey 2. In survey 1, I have a “Text Entry” field where the participants can enter multiple lines and it will be displayed in Survey 2. I used JS and Query String to pass the data from survey 1 to survey 2. 

 

Piped text value of the text entry field in survey 1: ${q://QID142/ChoiceTextEntryValue}

Imbedded data in survey 2:  MM_Text

 

My JS code is given below. It is added to the last question of Survey 1.  

 

jQuery("#NextButton").on('click', function() {

var MM_Val=    "${q://QID141/SelectedChoicesRecode}";

var MM_Text=    "${q://QID142/ChoiceTextEntryValue}";

 

var fullURL = 'Survey 2 link?'
            
            + 'MM_Val=' + MM_Val
    
            + '&MM_Text=' + MM_Text;

var link1 = window.open(fullURL, '_parent');
    
    });

 

The code works fine and trigger survey 2 at the end of survey 1, as long as a participant do not use ENTER while writing in the Text Entry field of Survey 1. It accepts alpha numeric values, emojis, SPACE everything, except for ENTER. If someone writes a paragraph using ENTER instead of SPACE, the survey ends at the THANK YOU page of survey 1 and does not trigger Survey 2. How do I solve this issue? 

@MM77_C URLs typically do not support line breaks, so if the text entered in the "Text Entry" field in Survey 1 contains line breaks, it can break the URL and prevent it from working as expected. More than that, everytime you pull "${q://QID142/ChoiceTextEntryValue}" with a line break it break the JS also. So you should encode the line break where it got capture in QID142 (i.e get the input field value when next button press)so that it can be safe to be handle with JS and attached properly


Here I encode it with URIComponent but also have to change the %0A to <br> because when you send it through URL It got automatically decode

Qualtrics.SurveyEngine.addOnReady(function()
{

jQuery("#NextButton").on('click', function() {
var textarea = document.getElementById('QR~QID142');
var textValue = textarea.value;
var encodedText = encodeURIComponent(textValue);
encodedText = encodedText.replace(/%0A/g, '<br>');
console.log(encodedText);
Qualtrics.SurveyEngine.setEmbeddedData('MM_Text', encodedText);
})

});

So when you attach it to the link, make sure to pull out the embedded data 

var MM_Text=    "${e://Field/MM_Text}";

Now to show it in survey 2 you have to make some replace to the mess up specical character to be HTML code

Qualtrics.SurveyEngine.addOnload(function() {
var MM_Text = "${e://Field/MM_Text}";
var decodedText = MM_Text.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
document.getElementById("myElement").innerHTML = decodedText;
});

This will push the HTML directly to a div with id = myElement in the question and bring the line break with it


@MM77_C URLs typically do not support line breaks, so if the text entered in the "Text Entry" field in Survey 1 contains line breaks, it can break the URL and prevent it from working as expected. More than that, everytime you pull "${q://QID142/ChoiceTextEntryValue}" with a line break it break the JS also. So you should encode the line break where it got capture in QID142 (i.e get the input field value when next button press)so that it can be safe to be handle with JS and attached properly


Here I encode it with URIComponent but also have to change the %0A to <br> because when you send it through URL It got automatically decode

Qualtrics.SurveyEngine.addOnReady(function()
{

jQuery("#NextButton").on('click', function() {
var textarea = document.getElementById('QR~QID142');
var textValue = textarea.value;
var encodedText = encodeURIComponent(textValue);
encodedText = encodedText.replace(/%0A/g, '<br>');
console.log(encodedText);
Qualtrics.SurveyEngine.setEmbeddedData('MM_Text', encodedText);
})

});

So when you attach it to the link, make sure to pull out the embedded data 

var MM_Text=    "${e://Field/MM_Text}";

Now to show it in survey 2 you have to make some replace to the mess up specical character to be HTML code

Qualtrics.SurveyEngine.addOnload(function() {
var MM_Text = "${e://Field/MM_Text}";
var decodedText = MM_Text.replace(/&lt;/g, '<').replace(/&gt;/g, '>');
document.getElementById("myElement").innerHTML = decodedText;
});

This will push the HTML directly to a div with id = myElement in the question and bring the line break with it

Hi Nam, 

Thank you so much for the reply. I understand the issue. 

I am confused about where to place the codes that you wrote. Please let me know if the below understanding is correct. 

  1. The 1st code: JS at the end of Survey 1.
  2. The 2nd one var MM_Text=    "${e://Field/MM_Text}"; Not sure where to add it.

My survey 2 link :       Survey 2 link?MM_Val=${q://QID102/ChoiceGroup/SelectedChoices}&MM_Text=${q://QID135/ChoiceTextEntryValue}

Survey 2 has the embedded data MM_Text in the survey flow and it is attached to Q1 (Your response from Survey 1 is: "${e://Field/MM_Text}") of survey 2. The variable MM_Text is only an embedded data that gets its value from Survey 2 URL as mentioned above. It does not appear on the user interface. 

  1. Same Question about the 3rd one. Where to add it? Q1 of Survey 2 JS? 

Please let me know. 

 

Regards

MM


@MM77_C I thought you know coding, anyway.
Like what I said, the 1st one is you have to get value where it’s been input, in the question with QID142. 2nd, when you attach the link, at your code now pull it from this embedded data instead of question response.(i.e change your code)
3rd one in the question where you want to pull in the data but you have to add a HTML element for the data to be pull in.
 


@MM77_C I thought you know coding, anyway.
Like what I said, the 1st one is you have to get value where it’s been input, in the question with QID142. 2nd, when you attach the link, at your code now pull it from this embedded data instead of question response.(i.e change your code)
3rd one in the question where you want to pull in the data but you have to add a HTML element for the data to be pull in.
 

I don’t know coding. I did everything using ChatGPT. 

That is why I took some time to get back to you. I had some issues with the placement of the codes you provided. Anyway it is done now. Thank you so much. My survey is working fine (there is a small issue but I am ignoring that for now). 

Your assistance has been the most effective I've encountered on this platform—clear, concise, and successful on the first attempt. Thank you.


Leave a Reply