471,887 Members | 1,459 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

getting the children

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
Jul 23 '05 #1
3 2024
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.
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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.
Jul 23 '05 #3
On Sun, 19 Sep 2004 16:02:50 +1000, Fred Oz <oz****@iinet.net.auau> wrote:

[snip]
On Sat, 18 Sep 2004 18:39:13 +0200, Wim Roffal
<wi*************@nospamm-planet.nl> wrote:
[snip]
A related question. If I have an element, how can I see which type
(a, div, input,etc) it is?

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

303 posts views Thread by mike420 | last post: by
1 post views Thread by Todd Cary | last post: by
6 posts views Thread by Luke Dalessandro | last post: by
12 posts views Thread by Sunny | last post: by
27 posts views Thread by Richard Blewett [DevelopMentor] | last post: by
3 posts views Thread by Andrej Pavlovic | last post: by
4 posts views Thread by Andrew May | last post: by
5 posts views Thread by Sin Jeong-hun | last post: by
1 post views Thread by duane.lortie | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.