I have a textarea for users to input comments and a button to submit using jQuery’s .post() and JSON:
$('#submit').click(function () {
var comment = {};
comment.Author = $("#author").val();
comment.Email = $("#email").val();
comment.WebSite = $("#url").val();
comment.Body = $("#comment").val(); // textarea
$.post('/ajax/comments', comment, parseComment, 'json');
But $("#comment").val() does not seem to preserve newlines (the input is losing newlines). How do I get this to work?
A textarea does not insert
<br>tags in a users input for line breaks, rather it simply includes the newline character\n.This snippet will show an alert box when you click the button, which has the broken line.
If you need to display these in the html page, you need to replace the
\nwith<br>yourself.