Walking the Child nodes

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

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
 
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)
 
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
 
bruce barker said:
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)

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
 
Back
Top