I have a text area in which user enters text. I want to retrieve the text as user entered including the new line character which is automatically wrapped. how to get textarea's text using jQuery with newline character preserved?
you need to replace the "\n" with br tags or wrap the content in pre to see the nextlines.
Here's a sample code:
assuming you have your page markup like this:
<div id="res"></div>
<textarea id="txtarea"></textarea>
<button id="btn">go</button>
then this is the jquery code:
$(document).ready(function() {
var btn = $('#btn');
var txtarea = $('#txtarea');
var result = $('#res');
btn.click(function() {
var val = txtarea.val().replace(/\n/g, '<br/>');
result.html(val);
return false;
});
});
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With