admin管理员组

文章数量:1310099

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..

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..

Share Improve this question edited Aug 25, 2013 at 4:12 Konsole 3,4753 gold badges30 silver badges41 bronze badges asked Aug 25, 2013 at 4:04 Jhonatan G.Jhonatan G. 951 gold badge3 silver badges11 bronze badges 1
  • I think it would be better to store the text as is and then call nl2br() when needed. – Matteo Tassinari Commented Aug 25, 2013 at 6:05
Add a ment  | 

3 Answers 3

Reset to default 3

Try this

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

Fiddle http://jsfiddle/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 pare 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

try

document.getElementById('textareaid').innerHTML;

you need to replace 'textareaid' with the actual id. since you say you already have the data in a string, but its not formatted right, to turn the <br> into newlines, you can use this

textString=textString.replace(/<br>/g,"\n");

It works for me....

$("#id").val("<?php echo str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br>",$value); ?>".replace(/<br\s*[\/]?>/gi, "\n"));

本文标签: JavaScriptJQueryline break in textarea don39t get n and get ↵Stack Overflow