Forum Moderators: open

Message Too Old, No Replies

Preserving linebreaks from nodeValue in IE

         

mehh

5:54 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



My problem is I have a element and Iwould like to split the value of it into an array but when I extract the text IE seems to ignore the linebreaks so
Hello,
How are you today?
Becomes
Hello, How are you today?

I have used the following methods:

var val=e.firstChild.nodeValue.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
var val=e.innerHTML;
var val=e.innerText ? e.innerText : e.firstChild.nodeValue.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");

None worked.

Thanks for any help in advance.

MarkFilipak

6:39 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



myArray = "Hello,\\nHow are you today?".split(/\\n/g);

OH! Sorry. Now I see what you're saying (...writing).

Ya can't do that. Once the text content has been digested as HTML, unless it's contained within a pre-element, the whitespace has been stripped. What's gone is gone. Sorry.

[edited by: MarkFilipak at 6:49 pm (utc) on Mar. 6, 2008]

penders

11:53 am on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you grab the content from the page, the CRLF (or CRs or LFs - depending on OS) should still be present (from a <textarea> element?). However, if you were to then output this again to the page (into a DIV perhaps) then any white-space is naturally truncated. You could try CSS...
white-space:pre
to preserve white-space on the element, or convert CRs to <br> before displaying....?

mehh

3:56 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



To clarify, I have a
<code>
element that is styled with
code{white-space:pre}
. Although IE is displaying this correctly, when the script trys to get this information it ends up like above. Opera handles this just fine and splits where its supposed to, I haven't had a chance to test in FF.

chriswo

5:39 am on Mar 9, 2008 (gmt 0)

10+ Year Member



If you have an element like this:

<code>
Line 1

Line 3

Line 5
</code>

When you access the code tag's .innerHTML, IE will strip the empty line feeds and return this:

Line 1 Line 3 Line 5

There isn't a way to get .innerHTML (that I know of) in IE with line breaks. The parent element (code, pre, p, etc.) doesn't matter.

mehh

10:07 am on Mar 9, 2008 (gmt 0)

10+ Year Member



I've now got it working useing a textarea inseted of a code element and useing the value property but I'd still rather use the code element for sementic reasons. Does anyone know a way to change the tagName or nodeName on the fly?

MarkFilipak

10:18 am on Mar 9, 2008 (gmt 0)

10+ Year Member



I wrote a bit here ... but never mind. I was misunderstanding the problem and what you've done already.

I've never tried overloading a tagName but I'd be very surprised if it isn't read-only and causes an exception: attempt to set a property that has only a getter.

[edited by: MarkFilipak at 10:31 am (utc) on Mar. 9, 2008]