Results 1 to 8 of 8

Thread: Array question

  1. #1
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Array question

    I have a bunch of DIVs inside a container DIV and I want to get the number of divs inside the container div dynamically (since the # of divs could change). Could I do this with arrays?

    HTML Code:
    <div class="container">
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    </div>
    Thanks a bunch!

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Code:
    <div class="container" id="container">
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    <div class="div1"></div>
    </div>
    <script type="text/javascript">
    window.onload = function() {
        var numDivs = document.getElementById("container").getElementsByTagName("div").length;
        alert(numDivs);
    };
    </script>
    Edit: Fixed typo.
    Last edited by magicyte; 03-22-2009 at 05:14 AM.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You can't actually use arrays, no. But the code presented by magicyte is a workable approach to this issue (even though it has at least one typo). However, it doesn't use arrays. It uses a nodeList. A nodeList varies from an array in that, once it is established, if something in the document tree changes that affects it, it will be automatically updated. A pure array, once established, will not change unless you directly alter it.

    Now, to be clear (hopefully), in magicyte's code numDivs will not change if the DOM tree is changed. But if you set like:

    Code:
    var mydivs = document.getElementById("cont").getElementsByTagName("div");
    Assuming there is an element with the id 'cont' (that's the typo in magicyte's code, there is no such element), then if that list of divisions changes, so will the mydivs variable.
    Last edited by jscheuer1; 03-21-2009 at 05:33 AM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Oh right I forgot all about tagnames. Thanks a ton both of you!

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There are other differences between NodeLists and Arrays too. A NodeList, for example, does not have the methods prototyped onto Arrays, such as slice().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    There are other differences between NodeLists and Arrays too. A NodeList, for example, does not have the methods prototyped onto Arrays, such as slice().
    A good point. A nodeList is not an array. It can be parsed like one (as long as one doesn't alter it in the process), but that's about it.

    But if one understands the differences, there can be advantages in the nodeList, as well as in the array. By simply taking advantage of each one's characteristics and working with them, either can be a powerful tool. For example, a nodeList's characteristic of being constantly updated can be utilized in some interesting ways. But, at the most basic level they both are roughly equivalent in nature. Also, a nodeList may be used to generate a true array. Unfortunately - at least as far as I know, the reverse is not the case.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A good point. A nodeList is not an array. It can be parsed like one (as long as one doesn't alter it in the process), but that's about it.
    Specifically, the only things they share are numerically-indexed elements and a dynamically updated length property.

    Also, a nodeList may be used to generate a true array. Unfortunately - at least as far as I know, the reverse is not the case.
    Well, a NodeList needs to be attached to a DOM object. The handiest would seem to be a document fragment:
    Code:
    var Dom = {
      arrayToNodeList: function(arr) {
        var frag = document.createDocumentFragment();
    
        for (var i = 0, e; e = arr[i]; ++i)
          frag.appendChild(e);
    
        return frag.childNodes;
      }
    };
    Last edited by Twey; 03-23-2009 at 01:45 AM. Reason: Having an incrementor is usually good.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. The Following User Says Thank You to Twey For This Useful Post:

    jscheuer1 (03-22-2009)

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Another tool for the arsenal.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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