javascript for URl encoding a respondent's answer | XM Community
Solved

javascript for URl encoding a respondent's answer

  • 5 May 2019
  • 1 reply
  • 58 views

Hello,
I need to be able to send email triggers that contain a URL with piped text using a text response from users. When the response contains spaces, it causes an error. It seems I need URL encode the text response for the piped text in the email trigger. I am totally unfamiliar with javascript but it seems it's the only way to accomplish this. Can anyone provide the solution I need? Instructions for implementing would be much appreciated as well.
icon

Best answer by WaterSampler 5 May 2019, 14:01

View original

1 reply

Badge +1
Add this to the bottom area of the javascript screen for the question that has the code you want to encode for a URL:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var qid = this.questionId;
var textFld = document.getElementById('QR~' + qid);
var textRsp = textFld.value;
var altName = encodeURIComponent(textRsp);
Qualtrics.SurveyEngine.setEmbeddedData("AltSiteName", altName);
//console.log('url name is: ' + altName)
});

What it does is to read the text response, encodes it for URL, and then sets it into an embedded data variable called altName. You can then pipe altName into your final url. If you have several questions, you will have to use different names for the embedded data.

Leave a Reply