Connecting Tech Pros Worldwide Help | Site Map

Need a little help with NODES

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:31 PM
Charlie T
Guest
 
Posts: n/a
Default Need a little help with NODES

Hello,

I am a little confused... please help:

-----------------------------------
<DIV id="01">
<DIV id="aNum"></DIV>
<DIV id="bNum"></DIV>
<DIV id="cNum"></DIV>
</DIV>
------------------------------------

is <DIV id="01"> the parent node of <DIV id="aNum"></DIV> ?

If so... How do I access each child using DOM...

shoud this work?:
a = document.getElementById('DIV');
b = a(0).childNodes.length;

Thanks...

  #2  
Old July 23rd, 2005, 01:32 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: Need a little help with NODES

On 8 Sep 2004 15:46:00 -0700, Charlie T <charlie.topjian@gmail.com> wrote:
[color=blue]
> Hello,
>
> I am a little confused... please help:
>
> -----------------------------------
> <DIV id="01">
> <DIV id="aNum"></DIV>
> <DIV id="bNum"></DIV>
> <DIV id="cNum"></DIV>
> </DIV>
> ------------------------------------
>
> is <DIV id="01"> the parent node of <DIV id="aNum"></DIV> ?[/color]

It is the parent of all of the <x>Num DIV elements. However, you should be
aware that you cannot start an id attribute value with a number. It must
begin with a letter.
[color=blue]
> If so... How do I access each child using DOM...
>
> shoud this work?:
> a = document.getElementById('DIV');
> b = a(0).childNodes.length;[/color]

No. You'll either want

var a = document.getElementById('divId');

or

var a = document.getElementsByTagName('DIV');

The first returns a reference to a specific DIV. The second returns a
(possibly empty) collection of all DIV elements in the document. Once you
have a reference to a particular node, then yes, you can use the
childNodes property to access the children of that node.

Another thing to note is that you don't index collections with parentheses
(). You use square brackets []. Whilst IE supports the use of parentheses,
that vast majority of browsers do not (and *should* not).

Finally, all of this has an important caveat: not all browsers you can
encounter on the Web will support the DOM. To be safe, you should always
test for a feature before using it.

if(document.getElementById)
var d = document.getElementById('myDiv');

if(d && d.childNodes) {
// Do stuff with the childNodes collection
}
}

If you haven't already, do read the FAQ entry on feature detection
(section 4.26):

Good luck,
Mike


clj FAQ:
<URL:http://jibbering.com/faq/>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
  #3  
Old July 23rd, 2005, 01:32 PM
Lee
Guest
 
Posts: n/a
Default Re: Need a little help with NODES

Charlie T said:[color=blue]
>
>Hello,
>
>I am a little confused... please help:
>
>-----------------------------------
><DIV id="01">
><DIV id="aNum"></DIV>
><DIV id="bNum"></DIV>
><DIV id="cNum"></DIV>
></DIV>
>------------------------------------
>
>is <DIV id="01"> the parent node of <DIV id="aNum"></DIV> ?[/color]

Yes.

[color=blue]
>If so... How do I access each child using DOM...
>
>shoud this work?:
>a = document.getElementById('DIV');
>b = a(0).childNodes.length;[/color]

No, it shouldn't.
"DIV" is not the ID of any of your elements.
"a" is not a function, so "a(0)" is undefined.

a = document.getElementById('01');
b = a.childNodes.length;

Note that element '01' in your example has 7 childNodes,
including the text nodes surrounding the <div> nodes.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.