I have a textarea where I insert \\n
when user presses enter. Code from this textarea is sent to a WCF service via jQuery.ajax()
. I cannot save
The following will replace all instances of \n
with a <br />
:
while (message.indexOf("\\n") !== -1) {
message = message.replace("\\n", "<br />");
}
Like said in comments and other answer, it's better to do it on server side.
However if you want to know how to do it on clientside this is one easy fix:
textareaContent.replace(/\\n/g, "<br />");
Where textareaContent
is the variable with the data in the textarea.
Edit: Changed so that it replaces globally and not only first match.
You can use a simple javascript string function.
string.replace("\n", "<br>")
If you support PHP you should check this out: http://php.net/manual/en/function.nl2br.php
you can use javascript built in replace function with a little help of regex, for example
$('#input').val().replace(/\n\r?/g, '<br />')
this code will return all enters replaced with <br>
Replace with global scope
$('#input').val().replace(/\n/g, "<br />")
or
$('#input').val().replace("\n", "<br />", "g")