Solved

display newline CR within <textarea>

Posted on 2009-04-03
9
711 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
100% tall div not scrollable on iPhone 3 20
Media Queries  not Firing 6 17
Using toggleClass to resize Bootstrap columns 7 18
Fix Form size HTML 16 14
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

860 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