[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

display newline CR within <textarea>

Posted on 2009-04-03
9
Medium Priority
?
716 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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

867 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