[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

display newline CR within <textarea>

Posted on 2009-04-03
9
Medium Priority
?
715 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: 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

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

649 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