Search code examples
javascriptjqueryajaxtextarealine-breaks

Jquery, line break in textarea.. don't get \n and get ↵


I am trying to get value of a textarea with line break. When I debug, the value is this way in jquery. the value stored in a variable like this:

"test<br>
test<br>
test"<br>
In .value of valueOf is: 'test↵test↵test'.

I wonder how can I convert it to \n in order to insert line break.. I'm using jquery to get the value and send by ajax to php!

thanks.. :)
Sorry my english..


Solution

  • Try this

    val=document.getElementById('recommend').value;
    val = val.replace(/<br\s*\/?>/mg,"\n");
    

    Fiddle http://jsfiddle.net/eSub4/1/

    I have put a couple of console.log in fiddle to show you how new line(\n) and html line break show up in console and compare it with the text from textarea to see that you are getting new line (\n) for text in textarea after using the regex. Keep firebug open to see the output