How do I get my textarea to preserve newlines when using jQuery and JSON?
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.
use $('#comment').val().replace( /\n/g, '<br \\>' );
.