Questions On Enumerating DOM childNodes

L

Louis

Please help.....

I am starting to learn using javascript on DOM. One of the first things
I tested was listing all the child nodes from an element. Like ->

*html code:
<div id="pagetitle">
<span>My Text</span>
</div>

In my javascript I tried to list all the child nodes from <div>, Firefox
and Opera gave me 3 -- 1)text node with a null value, 2)span, 3)text
node with a null value.

It look to me that the browsers treated the line break as a text node
with no value!!

If I modified the html code to :
<div id="pagetitle"><span>My Text</span></div>

Then the browsers gave me the right answer: 1

They still gave me a wrong answer of 3 if I put a space around the
<span> element like this:
<div id="pagetitle"> <span>My Text</span> </div>

What did I do wrong? I would still want the elements to be on their own
lines.

Thank you.
 
V

VK

Louis said:
Please help.....

I am starting to learn using javascript on DOM. One of the first things
I tested was listing all the child nodes from an element. Like ->

*html code:
<div id="pagetitle">
<span>My Text</span>
</div>

In my javascript I tried to list all the child nodes from <div>, Firefox
and Opera gave me 3 -- 1)text node with a null value, 2)span, 3)text
node with a null value.

It look to me that the browsers treated the line break as a text node
with no value!!

If I modified the html code to :
<div id="pagetitle"><span>My Text</span></div>

Then the browsers gave me the right answer: 1

They still gave me a wrong answer of 3 if I put a space around the
<span> element like this:
<div id="pagetitle"> <span>My Text</span> </div>

What did I do wrong? I would still want the elements to be on their own
lines.

see
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/16964dfe3f4809a0>
 
R

RobG

Louis said:
Please help.....

I am starting to learn using javascript on DOM. One of the first things
I tested was listing all the child nodes from an element. Like ->

*html code:
<div id="pagetitle">
<span>My Text</span>
</div>

In my javascript I tried to list all the child nodes from <div>, Firefox
and Opera gave me 3 -- 1)text node with a null value, 2)span, 3)text
node with a null value.

Safari will too.
It look to me that the browsers treated the line break as a text node
with no value!!

If I modified the html code to :
<div id="pagetitle"><span>My Text</span></div>

Then the browsers gave me the right answer: 1

You mean the answer you expected. :)
They still gave me a wrong answer of 3 if I put a space around the
<span> element like this:
<div id="pagetitle"> <span>My Text</span> </div>

What did I do wrong? I would still want the elements to be on their own
lines.

Some browser developers decided to preserve whitespace within documents
using #text nodes as they believe that is in accordance with the
specification. It applies to browsers other than Gecko-based ones
(such as Firefox and Mozilla) and Opera. IE behaves differently and
removes such whitespace from the DOM tree and from the innerHTML
property also.

Regardless of which approach you think is right or wrong, the fact is
that you need to deal with it and know that when traversing down the
DOM tree you may encounter #text nodes in some browsers but not others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top