1

Possible Duplicate:
The .val() of a textarea doesn't take new lines into account

I have a text area when i type a message in the text area with newlines and get the value back from the text area the newlines are not preserved and the text is written as one line. how do i get the newlines from the text area?I am using IE 9 and Mozilla, Opera, Safari

var message = $(".chatpaneltext textarea").attr("value");
Community
  • 1
  • 1
redoc01
  • 2,107
  • 5
  • 32
  • 64
  • What language? PHP? Something else? – Ariel Dec 13 '11 at 00:46
  • Could we see the code please? – Jeffrey Sweeney Dec 13 '11 at 00:46
  • How are you determining that there are no line break in the data? At the moment you are just loading it into a variable. – Quentin Dec 13 '11 at 00:49
  • Duplicate: [http://stackoverflow.com/questions/8481025/the-val-of-a-textarea-doesnt-take-new-lines-into-account](http://stackoverflow.com/questions/8481025/the-val-of-a-textarea-doesnt-take-new-lines-into-account) – Will Dec 13 '11 at 00:50

3 Answers3

3

Make sure you are intepreting your line breaks \r\n as <br /> tags.

text.replace(/\n\r?/g, '<br />'); 

If you're using PHP there is the nl2br() function. Or any other language, the above regex should work.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
1

You have to convert new lines to html
tags with nl2br() function before outputting.

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
1

if you're using PHP to process the input from your textarea, you can use the nl2br() function to convert newline characters to <br/>. You can also accomplish this in javascript using string.replace(/\n/g, '<br/>');.

Aaron
  • 5,137
  • 1
  • 18
  • 20