View Full Version : Making an Array of Total Nested Links

10-05-2008, 09:54 PM
Okay, so here it goes:

Say I have a list of links inside a DIV with the ID "container":

<div id="container">
<li><a href="one.html">One</a></li>
<li><a href="two.html">Two</a></li>
<li><a href="three.html">Three</a></li>

And I want to make an array of all the links inside, but none outside, without using any preset variables (like making a variable called "links" holding the number "2" and then making an array called "totalLinks(links)" or something.*) I want it to be slightly more dynamic so that I don't have to go back in to the script every time I add or delete a link from that list inside that DIV.

I tried doing something equivalent to this:

myLinks = document.getElementById('container').links

I did the "links" part at the end because when used generally (like document.links) it should return an array of all of the links within the page. So, I figured when I use it in this context it would return an array of only the links within the specified element. It didn't quite work for some reason, though.

The code I'm using this in is a lot larger than this, and since I'm new it's kind of messy, so trouble shooting with alert boxes and such (which I try to do when I normally hit something stupid like this) wasn't the most helpful.

So, my question is, would anyone here mind enlightening me? As in, what's wrong here that I'm overlooking, or is there an alternative to do the same thing I'm attempting with a relatively small amount of code and as dynamic as possible (without much markup)?

* I realize that "links" is a JavaScript reserved word- I was just using this as an example.

Arty Effem
10-06-2008, 01:12 AM
myLinks = document.getElementById('container').links

myLinks = document.getElementById('container').getElementsByTagName('a');