Questions On Enumerating DOM childNodes

Discussion in 'Javascript' started by Louis, Jan 7, 2007.

  1. Louis

    Louis Guest

    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.
    Louis, Jan 7, 2007
    #1
    1. Advertising

  2. Louis

    VK Guest

    Louis wrote:
    > 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>
    VK, Jan 7, 2007
    #2
    1. Advertising

  3. Louis

    RobG Guest

    Louis wrote:
    > 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.

    --
    Rob
    RobG, Jan 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    3,440
  2. Vincent De Baere

    xml.dom.minidom childnodes

    Vincent De Baere, Jan 18, 2004, in forum: Python
    Replies:
    2
    Views:
    1,372
    Vincent De Baere
    Jan 18, 2004
  3. Skip Montanaro
    Replies:
    4
    Views:
    951
    Uche Ogbuji
    Sep 4, 2004
  4. Omer  Ahmad
    Replies:
    0
    Views:
    400
    Omer Ahmad
    Aug 30, 2004
  5. Jake Barnes
    Replies:
    2
    Views:
    121
    GĂ©rard Talbot
    Feb 7, 2006
Loading...

Share This Page