Michael Winter wrote:
On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal
<wi*************@nospamm-planet.nl> wrote:
[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".
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.
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.