0

I have a textarea and I set its text to a string with \n in it. I would expect that to represent a line break.

$("#ConfirmEmailText").text("test\n\ntest\ntest");

However, I just get what looks like a tab rather than a new line.

If I add some additional text to the box, and press enter. I can see the text on seperate line. I then take the text in this box and send it in an email. In the email both the \n and my [enter]s from when I added some text show up as newlines in the email...

Why does this not display correctly in my textarea?

Thanks!

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • possible duplicate of [Show separation between newlines in textarea](http://stackoverflow.com/questions/5408950/show-separation-between-newlines-in-textarea) – Diodeus - James MacFarlane Apr 07 '11 at 14:53
  • @Diodeus: I don't think so, that question is about adding a paragraph marker at the hard break. I think the OP here just wants the hard breaks to be respected. – T.J. Crowder Apr 07 '11 at 15:02
  • @T.J. thats correct, when my string has a `\n` in it I want it to show up that same as if the user pressed `enter` within the the text area. – kralco626 Apr 08 '11 at 10:54
  • @people voting for close - It would be nice if you commented and said why you were going to vote to close and gave me a chance to respond... It's not a duplicate so i'm not sure why this would need to be closed... – kralco626 Apr 08 '11 at 10:56

3 Answers3

8

Set the value not the innerText;

$("#ConfirmEmailText").val("test\n\ntest\ntest");
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • This works, but using a variable instead of the text doesn't seem to work. I verified the value with an alert. – Yster Aug 28 '15 at 10:28
  • @Yster — Should work fine with a variable. You might want to [create a proper test case](http://sscce.org/) and then [ask a new question](/questions/ask) – Quentin Aug 28 '15 at 10:40
0

Did you try?

   text = text.replace(/(\r\n|\r|\n)/g, '\n');

Or use the DOM structure's .nodeValue property.

EDIT : i may have not been clear. Instead of using innerHTML to set the text try using nodeValue

Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
0

try

text = text.replace("\n","<br/>");
R3D3vil
  • 681
  • 1
  • 9
  • 22