473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Can't get carriage returns in div

I have a page with a div on it. The div displays a user comment. When
the user logs into this page, their current comment is pulled from a db
and displayed in the div. The user can edit the comment through a
pop-up that contains a textarea. When the user hits OK on the pop-up,
the text in the textarea is sent to a function on the main page. The
function inserts the text into the div's text node.

Please don't ask why I'm making this so complicated - there are other
things going on on the page and the pop-up that are irrelevant to my
problem.

Everything works perfectly except when the user puts carriage returns
in their comment. For some reason I can't get the carriage returns to
show up in the div. BUT! When they view the existing comment in the
div (the one pulled from the database) the carriage returns are
displayed. That initial display does not use any javascript - it's
just php so the page is initially rendered with the text in the div.

So it seems that when I programmatically put text into my div, the
carriage returns don't show. But if the page is rendered with the text
already there, the carriage returns do show.

This seems like it should be so simple, what am I doing wrong??? Do I
have to replace the carriage returns with a different character (e.g.
<br>, \n, \r, \n\r, \r\n)? Or is there a css setting for the div?

Any help would be greatly appreciated.

- Matt

Jun 22 '06 #1
7 11146
Ivo
<ma**********@gmail.com> schreef
Please don't ask why I'm making this so complicated - there are other
things going on on the page and the pop-up that are irrelevant to my
problem.
But may I ask why ...oh well.
Everything works perfectly except when the user puts carriage returns
in their comment. For some reason I can't get the carriage returns to
show up in the div. BUT! When they view the existing comment in the
div (the one pulled from the database) the carriage returns are
displayed. That initial display does not use any javascript - it's
just php so the page is initially rendered with the text in the div.
Look at the HTML source produced by the PHP. What carriage returns? Are they
<br/> tags, or perhaps <p>, or is the div styled with "whitespace:pre"
(which might be a solution towards your question)?
This seems like it should be so simple, what am I doing wrong??? Do I
have to replace the carriage returns with a different character (e.g.
<br>, \n, \r, \n\r, \r\n)? Or is there a css setting for the div?


If you 're using the innerHTML property of the div to insert the new
content, then
something like thestring.replace( /\r?\n/g, '<br>' ) might just be enough,
yes. A more standards-compliant appraoch might involve splitting the string
into an array and <p> elements for every bit.
Have you tried any of that replacing? What happened?
ivo
Jun 22 '06 #2

Ivo wrote:
<ma**********@gmail.com> schreef
Please don't ask why I'm making this so complicated - there are other
things going on on the page and the pop-up that are irrelevant to my
problem.


But may I ask why ...oh well.
Everything works perfectly except when the user puts carriage returns
in their comment. For some reason I can't get the carriage returns to
show up in the div. BUT! When they view the existing comment in the
div (the one pulled from the database) the carriage returns are
displayed. That initial display does not use any javascript - it's
just php so the page is initially rendered with the text in the div.


Look at the HTML source produced by the PHP. What carriage returns? Are they
<br/> tags, or perhaps <p>, or is the div styled with "whitespace:pre"
(which might be a solution towards your question)?
This seems like it should be so simple, what am I doing wrong??? Do I
have to replace the carriage returns with a different character (e.g.
<br>, \n, \r, \n\r, \r\n)? Or is there a css setting for the div?


If you 're using the innerHTML property of the div to insert the new
content, then
something like thestring.replace( /\r?\n/g, '<br>' ) might just be enough,
yes. A more standards-compliant appraoch might involve splitting the string
into an array and <p> elements for every bit.
Have you tried any of that replacing? What happened?
ivo

In the HTML source produced by the PHP the new line character used is
<br>. It works like a charm. When I replace \n and/or \r with <br>,
the <br> is displayed in the text - it is not processed as a new line.

I've tried pretty much every permutation of string.replace. No luck
yet.

Also, I'm not using innerHTML (trying to stay w3c compliant). I'm
directly changing the value of the text node within the DIV.

- Matt

Jun 22 '06 #3
Ivo
<ma**********@gmail.com> schreef
Ivo wrote:
<ma**********@gmail.com> schreef In the HTML source produced by the PHP the new line character used is
<br>. It works like a charm. When I replace \n and/or \r with <br>,
the <br> is displayed in the text - it is not processed as a new line.

<snip> I'm not using innerHTML (trying to stay w3c compliant). I'm
directly changing the value of the text node within the DIV.


The <br> is not a character, it 's four characters that make up a tag, some
say, or an element, say others. In any case, if it 's fed into a textnode,
well, yes, it will be treated as text, four characters of it.
What you want to do is:
- split your text into pieces at every occurance of a \n in the textarea
(carriage return)
- loop through the pieces, and in the loop:
- - add them to the div using document.createTextNode one by one
- - add a document.createElement( 'br' ) after each one

Or like I said earlier, create a series of <p> elements.
Example code, using a textarea but not in a popup, follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>Title goes here</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript">//<![CDATA[
function addtext() {

var s = document.getElementById( 'thetext' ).value.split( /\n/ ), i = 0,
d = document.getElementById( 'thediv' );

while( d.firstChild ) { d.removeChild( d.firstChild ); }
// This clears any text previously added; remove the line and
// the div will continue to grow each time the function is run.

do {
d.appendChild( document.createTextNode( s[i++] ) );
d.appendChild( document.createElement( 'br' ) );
} while( s[i] );

}
//]]></script>
</head><body>

<div>
<textarea rows="4" cols="40" id="thetext">
Line one

This is the thid line.
</textarea>
<input type="button" value="Set text" onclick="addtext();"/>
</div>
<div id="thediv">
</div>

</body></html>

Watch out during testing that you do not accidentally create infinite loops
in the 'do-while' block. It 's tricky construct. A loop that uses 'for' is
usually safer, but slower. Another increase in speed and efficiency might be
achieved by copying nodes using the cloneNode method instead of the repeated
calls to createTextNode and createElement. The worth of these considerations
does depend on the kind of the texts you expect from your users.
In any case, try to avoid the pop up window.
hth
ivo
http://4umi.com/web/javascript/
Jun 22 '06 #4

ma**********@gmail.com wrote:

[snip]
I've tried pretty much every permutation of string.replace. No luck
yet. Also, I'm not using innerHTML (trying to stay w3c compliant). I'm
directly changing the value of the text node within the DIV.


Changing the text node may be why you are having no luck. If you
insert "<br>" in the text node, then it will not be parsed as a BR
element, it will simply become - &lt;br&gt; - and show up as content
rather than mark-up.

Some options are:-

1. Try creating a PRE element and inserting the text as a Text node
of the PRE element, although I think Internet Explorer still strips out
\n\r when programmatically inserted into text nodes.

2. Use innerHTML

This is relatively widely supported in the latest browsers I think.

In which case when moving text from the textarea you will need to first
escape the text by replacing all "<" ">" and "&" characters in the
inputted text (so they are not wrongly seen as markup) and secondly do
the replace for \n and \r to <br>.

3. Manually create a series of Text and BR nodes.

I.e. split the user input with:-

var aLines=myString.split(/\r\r|\n\r|\r\n|\r|\n/)

and then for each line insert
- a Text node, and then a BR node,
- or alternatively a P node containing a Text node.

Regards

Julian Turner

Jun 22 '06 #5
Julian Turner wrote:
ma**********@gmail.com wrote:

Some options are:-

2. Use innerHTML

This is relatively widely supported in the latest browsers I think.


All the major browsers support it. They may differ slightly as to
exactly how they implement it - but for this sort of use, I doubt you'll
notice any difference.

It has the additional advantages of (a) being faster, and (b) using less
code.

--
"The most convoluted explanation that fits all the available and made-up
facts is the most likely to be believed by conspiracy theorists"
Jun 22 '06 #6

Tony wrote:
Julian Turner wrote:
ma**********@gmail.com wrote:

Some options are:-

2. Use innerHTML

This is relatively widely supported in the latest browsers I think.


All the major browsers support it. They may differ slightly as to
exactly how they implement it - but for this sort of use, I doubt you'll
notice any difference.

It has the additional advantages of (a) being faster, and (b) using less
code.

--
"The most convoluted explanation that fits all the available and made-up
facts is the most likely to be believed by conspiracy theorists"

Thanks to everyone for their helpful remarks. As an aside, this is the
first time I have ever seen posts on a web-related newsgroup
recommending a non-standards compliant solution!

But your arguments are compelling and I'm going to make the switch to
innerHTML. Thanks again!

- Matt

Jun 22 '06 #7
ma**********@gmail.com wrote:
Thanks to everyone for their helpful remarks. As an aside, this is the
first time I have ever seen posts on a web-related newsgroup
recommending a non-standards compliant solution!
The standards aren't always right :)
But your arguments are compelling and I'm going to make the switch to
innerHTML. Thanks again!


There is some good info about innerHTML at
http://www.quirksmode.org/js/events_advanced.html (scroll down) and
http://www.quirksmode.org/dom/innerhtml.html
--
"The most convoluted explanation that fits all the available and made-up
facts is the most likely to be believed by conspiracy theorists"
Jun 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Guy Verville | last post by:
I'm perplexed, I have several forms that seem to be ok, but what is sent by email doesn't contain all the Carriage returns sent. The form contains many fields and is sent as follow:...
4
by: Simon Harris | last post by:
Hi All, I am trying to write a function that will remove all carriage returns from a string, so that the string of words can be used as meta keywords. So far I have: Function...
4
by: Les Juby | last post by:
Can someone please help with a suggestion as to how I can keep the formatting (carriage returns) that the user enters into a memo field and then display that later. I figured I might be able to...
3
by: Dave | last post by:
I need to strip out carriage returns from a memo field. Basically, one of the commercial databases we have uses a memo field for the address, and what I'd like to do is get rid of the carriage...
3
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me...
1
by: I am Sam | last post by:
In ASP using VBScript to replace Chr(13) or Chr(10) with a <br> tag I would use the Replace method so that I can catch carriage returns from a memo field in MS Access or a Text field in MS SQL...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
4
by: Tony Girgenti | last post by:
Hello. When i look at a web form in design view, i have the option to view the HTML. Some of the HTML code is in one long string and hard to read. Is it OK to put in carriage returns and...
0
by: markus.shure | last post by:
Hi, I'm noticed a problem testing a JAX-WS client with a WSE server. The JAX-WS client adds carriage returns to a SOAP header element that is signed. This causes the WSE server to raise an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.