Walking the Child nodes

Discussion in 'ASP .Net' started by tshad, Sep 7, 2007.

  1. tshad

    tshad Guest

    I am trying to walk the Child nodes and am using the code:

    var nodes = document.forms[0].childNodes;
    alert("nodes = " + nodes + " nodes.length = " + nodes.length);
    for (var i=0; i < nodes.length; ++i)
    {
    alert("Inside for loop - i = " + i);
    alert("id: = " + nodes.id);
    alert("tag: " + node.tagName);
    }
    alert("length = " + document.addForm.length);

    From the first alert I get:

    nodes = [object] nodes.length = 16

    From the second alert I get:

    Inside for loop - i = 0

    From the 3rd alert I get:

    id: = undefined

    It goes no farther from here because the id is undefined.

    Why is that?

    What I am trying to do is find out the tag names and types as well ids
    (haven't got the type there yet).

    If I tak out the alert("tag... out I will get all the nodes as undefined.

    What am I doing wrong here?

    Thanks,

    Tom
     
    tshad, Sep 7, 2007
    #1
    1. Advertising

  2. tshad

    bruce barker Guest

    try spelling nodes correct and using indexer:

    var nodes = document.forms[0].childNodes;
    for (var i=0; i < nodes.length; ++i)
    {
    alert("Inside for loop - i = " + i);
    alert("id: = " + nodes.id);
    alert("tag: " + nodes.tagName);
    }

    note: id may not be defined if one doesn't exist. you can nodeName in
    place of tagName. this will only give immediate childNodes.

    -- bruce (sqlwork.com)


    tshad wrote:
    > I am trying to walk the Child nodes and am using the code:
    >
    > var nodes = document.forms[0].childNodes;
    > alert("nodes = " + nodes + " nodes.length = " + nodes.length);
    > for (var i=0; i < nodes.length; ++i)
    > {
    > alert("Inside for loop - i = " + i);
    > alert("id: = " + nodes.id);
    > alert("tag: " + node.tagName);
    > }
    > alert("length = " + document.addForm.length);
    >
    > From the first alert I get:
    >
    > nodes = [object] nodes.length = 16
    >
    > From the second alert I get:
    >
    > Inside for loop - i = 0
    >
    > From the 3rd alert I get:
    >
    > id: = undefined
    >
    > It goes no farther from here because the id is undefined.
    >
    > Why is that?
    >
    > What I am trying to do is find out the tag names and types as well ids
    > (haven't got the type there yet).
    >
    > If I tak out the alert("tag... out I will get all the nodes as undefined.
    >
    > What am I doing wrong here?
    >
    > Thanks,
    >
    > Tom
    >
    >
     
    bruce barker, Sep 7, 2007
    #2
    1. Advertising

  3. Depending on which browser you're using, text nodes may be identified as
    nodes (Mozilla), or not (IE). So, you will have some nodes that indeed have
    no id assigned.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP

    DSI PrintManager, Miradyne Component Libraries:
    http://www.miradyne.net

    "tshad" <> wrote in message
    news:%...
    >I am trying to walk the Child nodes and am using the code:
    >
    > var nodes = document.forms[0].childNodes;
    > alert("nodes = " + nodes + " nodes.length = " + nodes.length);
    > for (var i=0; i < nodes.length; ++i)
    > {
    > alert("Inside for loop - i = " + i);
    > alert("id: = " + nodes.id);
    > alert("tag: " + node.tagName);
    > }
    > alert("length = " + document.addForm.length);
    >
    > From the first alert I get:
    >
    > nodes = [object] nodes.length = 16
    >
    > From the second alert I get:
    >
    > Inside for loop - i = 0
    >
    > From the 3rd alert I get:
    >
    > id: = undefined
    >
    > It goes no farther from here because the id is undefined.
    >
    > Why is that?
    >
    > What I am trying to do is find out the tag names and types as well ids
    > (haven't got the type there yet).
    >
    > If I tak out the alert("tag... out I will get all the nodes as undefined.
    >
    > What am I doing wrong here?
    >
    > Thanks,
    >
    > Tom
    >
    >
     
    Kevin Spencer, Sep 7, 2007
    #3
  4. tshad

    tshad Guest

    "bruce barker" <> wrote in message
    news:...
    > try spelling nodes correct and using indexer:
    >
    > var nodes = document.forms[0].childNodes;
    > for (var i=0; i < nodes.length; ++i)
    > {
    > alert("Inside for loop - i = " + i);
    > alert("id: = " + nodes.id);
    > alert("tag: " + nodes.tagName);
    > }
    >
    > note: id may not be defined if one doesn't exist. you can nodeName in
    > place of tagName. this will only give immediate childNodes.
    >


    That fixed it.

    What do you mean by "immediate" childNodes?

    Thanks,

    Tom

    > -- bruce (sqlwork.com)
    >
    >
    > tshad wrote:
    >> I am trying to walk the Child nodes and am using the code:
    >>
    >> var nodes = document.forms[0].childNodes;
    >> alert("nodes = " + nodes + " nodes.length = " + nodes.length);
    >> for (var i=0; i < nodes.length; ++i)
    >> {
    >> alert("Inside for loop - i = " + i);
    >> alert("id: = " + nodes.id);
    >> alert("tag: " + node.tagName);
    >> }
    >> alert("length = " + document.addForm.length);
    >>
    >> From the first alert I get:
    >>
    >> nodes = [object] nodes.length = 16
    >>
    >> From the second alert I get:
    >>
    >> Inside for loop - i = 0
    >>
    >> From the 3rd alert I get:
    >>
    >> id: = undefined
    >>
    >> It goes no farther from here because the id is undefined.
    >>
    >> Why is that?
    >>
    >> What I am trying to do is find out the tag names and types as well ids
    >> (haven't got the type there yet).
    >>
    >> If I tak out the alert("tag... out I will get all the nodes as undefined.
    >>
    >> What am I doing wrong here?
    >>
    >> Thanks,
    >>
    >> Tom
    >>
     
    tshad, Sep 10, 2007
    #4
    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. asd
    Replies:
    3
    Views:
    452
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    537
    gavnosis
    Aug 2, 2003
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,180
    David Schwartz
    Dec 9, 2003
  4. Replies:
    1
    Views:
    604
    Soren Kuula
    Aug 24, 2006
  5. Replies:
    1
    Views:
    1,223
    Joe Kesselman
    Nov 1, 2006
Loading...

Share This Page