/    Sign up×
Community /Pin to ProfileBookmark

getting access to dom elements

Hello

Assume I have the following HTML tags:

[code]
<div id=”container”
<div id=”a” />
<div id=”b” />
<div id=”c”>
<div id=”d” />
</div>
<div id=”e” />
</div>
[/code]

I need to write a JavaScript code that does the following:
Get a reference to the “container” element
get an array of all its [b]direct[/b] children

Iterate over its children array and print their id value
In the example above, the direct children would be:
a, b, c and e

Note the [b]d is left out[/b] because it is not a direct child of the “container”

I need the code to run on both IE and Firefox
I need a solution that will work even if the container’s direct children are not div elements, but any html tags

any help would be appreciated

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayNov 09.2005 — Attached is something I wrote sometime back to explore the DOM on IE and let me compare it to what I could get from the DOM Inspector in FF. It runs on both and should be a little more than what you're asking for.

[upl-file uuid=92e1ca65-2fc4-415a-b5e5-476d00cefeda size=2kB]DomWalker.zip[/upl-file]
×

Success!

Help @jasongr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...