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 \n
in DB as it won't show in other applications consuming the service.
How can i replace \n
with <br />
tag?
solution
Well many of you tried and some got right with Javascript Regex with /g (global modifier). At the end i had \n inserted twice, i don't know why, my only guess is that jQuery on keypress event created double \n which i debug.
$('#input').keypress(function (event) {
if (event.which == '13') {
inputText = $('#input').val() + '\n';
$('#input').val(inputText);
}
});
#input
an<input type="text"/>
? Because if it is you will not get any\n
. If it is a textarea I suggest you convert the data when the user clicks the button to submit and not when typing. But better than that I would just convert on the server-side. – Circumambient