Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get textarea's text using jQuery with newline character preserved?

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?

like image 400
Elangovan Avatar asked Dec 09 '22 15:12

Elangovan


1 Answers

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.

like image 112
ricecake5 Avatar answered Dec 12 '22 04:12

ricecake5