Maintaining line breaks when "Essay" type is saved | XM Community
Skip to main content
Hi all, First time post as I'm a bit stumped. The issue: Qualtrics seems to not maintain formatting upon submission of multine/essay type questions. I thought I could tackle this with some jQuery but I'm having no luck still. My solution attempt: - Essay type question - on the question is the following JS/jQuery: Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ console.log("loaded question "+this.questionId); jQuery("#"+this.questionId+" textarea").on("blur", function() { jQuery(this).val().replace("\\n","<br>"); }); }); Qualtrics.SurveyEngine.addOnReady(function() { /*Place your JavaScript here to run when the page is fully displayed*/ }); Qualtrics.SurveyEngine.addOnUnload(function() { }); Coding steps: 1. Targetting the textarea 2. When it unfocuses 3. Change the value contained within I seem to be stuck on step 3 - the blur works fine. It's difficult to tell whether I've succeeded or not (recommendations here appreciated too - if there's a good function to present unencoded text so I can visibly spot <br\\> vs \\n ?) This whole scenario seems silly as "multi-line" fields are still "actually-really-one-line" fields. Any help much appreciated - completely new to the API, and rusty with my jQuery.
First, providing a string argument to .replace() only replaces the first instance. You need to use regex to do a global replace. Also, you never updated the value. Try this instead: ``` this.value = this.value.replace(/\\n\\r?/g, "<br>"); ``` If you want to do it so the respondent doesn't see it, use the addOnPageSubmit function instead of a blur event. I don't remember ever testing it, but if the Qualtrics server strips html tags from inputs (for security) you'll loose the breaks anyway.
Damn, Well I mean thank you for the information - that makes sense - but it looks like the later might hold true as far as stripping HTML :( Would be nice if Qualtrics only stripped out more potentially abusive elements like <iframe\\>s. Seems a bit overkill to strip out <br\\>s