Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Making an Array of Total Nested Links

    Okay, so here it goes:

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

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

    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.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Magistrate View Post
    myLinks = document.getElementById('container').links
    Code:
    myLinks = document.getElementById('container').getElementsByTagName('a');


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •