Adding/Insert Line Breaks into a Textarea
Seems like a simple enough problem to solve...just use \n or a br tag
right? ...wrong.
This markup <textarea>Test<br/>Newline</textarea> produces:
And the markup <textarea>Test\nNewline</textarea> produces:
The trick to inserting a new line is to use the character for a carriage return. I stumbled upon this when I ran into this problem. I opened a new notepad file and made a simple textarea. When typing in it, I noticed that when I press enter it goes to a new line (obviously), so I figured why not try to put a carriage return into the textarea. The carriage return representation is .
So the markup <textarea>Test Newline</textarea> produces:
And there you have it, simple problem to which the answer may take 30+ minutes to find.
Hopefully I save you that 30 mins ![]()
Comment, Enjoy ![]()
July 1st, 2009 - 03:58
I thought I was being stupid not being able to sort this using but your post saved me a lot of frustration.
Thanks very much
July 1st, 2009 - 04:23
oh trust me, i experienced the exact same frustration as you. thats why i made the post, to save people the time. glad i helped
September 2nd, 2009 - 10:43
Thanks Scott. I was just beginning to think I was going to waste ages on a silly problem like this and here’s the solution. Great help.
September 2nd, 2009 - 13:44
not a problem..i wasted an hour or 2 on it
.
Glad i could help!
October 16th, 2009 - 16:43
It’s doesn’t work in IE7…
Someone else has the same problem ?
October 16th, 2009 - 17:33
It works in my version of IE7/8 and FF3+.
October 16th, 2009 - 18:08
Mmmm i am using PHP xajax functions and a jQuery edit in place plugin… I think the Javascript is doing something wrong…
But you are right…
December 3rd, 2009 - 13:51
Thanks very much.
December 16th, 2009 - 18:31
You’re welcome very much.
July 1st, 2010 - 13:06
You saved my sanity here… I was struggling with this for a few hours, during an otherwise busy day, trying to replace linebreaks with tags, tags with linebreaks, etc etc… finally I did the replace you showed (in the PHP str_replace function I had to also discover that single quotes are not the same as double quotes to get the replace to work), and bam… linebreaks in text as they were supposed to be… thanks
July 2nd, 2010 - 00:18
No problem.
This had me going for a while too. Please consider making a donation if any of my content helps you!
Enjoy your day. THanks.