javascript - Jquery, line break in textarea.. don't get n and get ↵ - Stack Overflow

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

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"));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741836219a4368829.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信