Solved

display newline CR within <textarea>

Posted on 2009-04-03
9
712 Views
Last Modified: 2012-05-06
Hi Experts,

a user can enter a text into a textarea. the text could have line breaks.
after the insert the text is stored into a database with \n as line breaks.

example:
This is my
Text
with
a lot of
new lines


now, when the user likes to edit the text he entered before the line breaks are not display
within the textarea. when i output the text the text look like:
This is my\nText \nwith \na lot of \nnew lines


How can i display these newlines within the textarea.

this site do the same when i edit my comment. but how does it work?


0
Comment
Question by:bibabutze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 24064353
Don't know what language you use but in php you use nl2br() function

$text = "This is my\nText \nwith \na lot of \nnew lines";

$text = nl2br($text);

echo "$text";
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 24064374
use the <pre> tags to display text with linebreaks
0
 
LVL 10

Expert Comment

by:Hans Langer
ID: 24065500
If you need to do it on javascript, you must to reasign the text like this.
In vb you should replace "\n" with vbcrlf or chr(13).
<html><body>
<SCRIPT LANGUAGE="JavaScript">
onload = function(){
	document.getElementById('myTxt').value = 'Line1\nLine2\nLine3\nLine4'
}
</SCRIPT>
<textarea id="myTxt" style="width:400px;height:400px"></textarea>
</body></html>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24065703
GERENTE has it... chr(13) should do it in the textarea, using javascript:
document.getElementById('myTxt').value = 'Line1'+chr(13)+'Line2'+chr(13)+'Line3'+chr(13)+'Line4';

Open in new window

0
 
LVL 5

Expert Comment

by:Pantalaim0n
ID: 24066652
It doesn't really matter how you put the text back in the textarea, \n linebreaks should just work (tested it). My guess is that the backslash is still escaped when getting it out of the DB. Maybe a str.replace(/\\n/g,"\n") will do something here.
0
 

Author Comment

by:bibabutze
ID: 24067032
Hi
i tried the suggestions. but they did´t work.
i made a function to replace <br> to \n .For this line breaks are display within the textarea.
function rplnl(fieldname)
{ var field = gEBI(fieldname);
 if (field)
 { str = field.value;
 
  field.value =  str.replace(/<br \/>/g,"\n\g");
 }
 
}
my problem now. i use a standard function in my php frameset.when i but nl2br($str) it works. but i can´t do this at this place.  So i decide to use javascript to replace the newlines.
I only need one more thing. Which character do i have to replace in Javascript.
str.replace(/????????????????/g,"\n\g");  for newlines.
 
  i already tried to find it out with str.charCodeAt(x). But it only shows 32 for  a newline but thats only a whitespace.
Any suggestions?
0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24067850
chr(13)
0
 

Author Comment

by:bibabutze
ID: 24070696
that does´t work
function rplnl(fieldname)
{ var field = gEBI(fieldname);
if (field)
{ str = field.value;

  field.value =  str.replace(/chr(13)/g,"\n\g");
}

}
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
ID: 24073863
i think you've got it upside down... the replace is in the following manner:

replace(string to find, string to replace with)...

simply invert your vars, and it should work... like this:

 field.value =  str.replace("\n\g",/chr(13)/g);

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question