Solved

display newline CR within <textarea>

Posted on 2009-04-03
9
708 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
Comment Utility
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
Comment Utility
use the <pre> tags to display text with linebreaks
0
 
LVL 10

Expert Comment

by:Hans Langer
Comment Utility
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
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:Pantalaim0n
Comment Utility
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
Comment Utility
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
Comment Utility
chr(13)
0
 

Author Comment

by:bibabutze
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now