Walking the Child nodes

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
 
B

bruce barker

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)
 
K

Kevin Spencer

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
 
T

tshad

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
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,833
Latest member
BettyeMacf

Latest Threads

Top