getting the children

W

Wim Roffal

I am having a <div> with <a>s and <div>s inside it.

I am trying to read in all the elements in the right order.

With mydiv.getElementsByTagName("a") I can get the elements of a specific
tag like "a", but I want all types.

I tried getChildNodes but that doesn't seem to be widely supported.
childNodes[] seemed to do something but I couldn't get it working when I
wanted to browse through the elements.

What is the best way to do this?

A related question. If I have an element, how can I see which type (a, div,
input,etc) it is?

Thanks,

Wim
 
M

Michael Winter

On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal

[snip]
I tried getChildNodes but that doesn't seem to be widely supported.

Probably because it doesn't exist. It's not part of Microsoft's DOM, or
the standardised W3C DOM (unless it's in Level 3).
childNodes[] seemed to do something but I couldn't get it working when I
wanted to browse through the elements.

What is the best way to do this?

Mind showing what you were trying to do? The childNodes collection is your
best bet.
A related question. If I have an element, how can I see which type (a,
div, input,etc) it is?

Use the nodeName property. This will return a case-sensitive string with
the element name. In HTML, the string will always be in uppercase.

Hope that helps,
Mike
 
F

Fred Oz

Michael said:
On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal

[snip]
I tried getChildNodes but that doesn't seem to be widely supported.


Probably because it doesn't exist. It's not part of Microsoft's DOM, or
the standardised W3C DOM (unless it's in Level 3).
childNodes[] seemed to do something but I couldn't get it working when
I wanted to browse through the elements.

What is the best way to do this?


Mind showing what you were trying to do? The childNodes collection is
your best bet.

To help you out, childNodes returns a collection,
you can do something like:

var a = document.getElementById('aDiv').childNodes;
for (var i=0; i<a.length; ++i) {
// do something to each child
}

Other useful methods are firstChild and nextSibling - which is
really useful if you start at some unknown point in a DOM and
just want the next sibling. Say you want to find the next one
after "a" above without having to find the parentNode, work out
the index of "a", then get the next one. It's all
done by "var b = a.nextSibling".
Use the nodeName property. This will return a case-sensitive string
with the element name. In HTML, the string will always be in uppercase.

There is also nodeType, but different browsers seem to put
different node types in some places so it may not be that
useful for you.


Cheers, Fred.
 
M

Michael Winter

[snip]
[snip]
[snip]

There is also nodeType, but different browsers seem to put
different node types in some places so it may not be that
useful for you.

The nodeType property doesn't return the information the OP wanted,
though. It returns quite literally the type: element, comment, DOCTYPE,
text, etc. However, that information does prove very useful when iterating
through the tree when looking for generic types. If you're looking for a
specific element, such as an INPUT, there isn't much need for it.

Mike
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top