views:

39

answers:

2

I have an textarea for user to input comment and a button to submit using jQuery .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() doese not seem to preserve newlines so how do I get this to work?

Edit: the input is losing newlines.

A: 

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.

<script type="text/javascript">
  $(document).ready(function(){
    $('#submit').click(function() { alert($('#comment').val()); })
  });
</script>

<textarea id='comment'></textarea>
<input type='submit' id='submit'/>

If you need to display these in the html page, you need to replace the \n with <br> yourself.

Chadwick
A: 

Is it losing new lines? See this post maybe?

rchern