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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Traversing the DOM to capture all the DIVs

    I am looking for a way to capture all the DIV information on a DOM. This the code I got so far, but I need some help.


    var divs = document.getElementsByTagName("div");
    for (var i = 0, l = divs.length; i < l; i++) {
    d1.write (divs[i].attributes.length+"-attribute length<BR>");
    d1.write (divs[i].attributes.item(1)+"-attribute value<BR>");
    d1.write (divs[i].childNodes.length+"-ChildNode length<BR>");
    d1.write(divs[i].childNodes.item(1)+"-ChildNodes value<BR>");
    }

    I am hitting other objects, and I am not sure how to write the code to dive into those objects.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    1. According to W3C standards, attributes is an unordered collection of attributes, not an array, thus, in theory, attributes has no length.
    2. Unfortunately, the implementation of attributes is buggy in all the browsers. Some browsers take it as an array, some don't. Older versions of IE do not consider style or events as attributes (corrected partially in IE9). IE7 counts all the possible attributes, not only the effectively set.

    There are many reasons for attributes is not cross-browser trusty.

    What, in fact, is your final aim? What do you rally want to do?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by Kor View Post
    1. According to W3C standards, attributes is an unordered collection of attributes, not an array, thus, in theory, attributes has no length.
    just because it’s not an array doesn’t mean it doesn’t have a length. according to the DOM specs, a NamedNodeMap (the attribute list) does have a length*.

    * - from the specs:
    Objects contained in an object implementing NamedNodeMap may also be accessed by an ordinal index, but this is simply to allow convenient enumeration of the contents of a NamedNodeMap, and does not imply that the DOM specifies an order to these Nodes.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Dormilich View Post
    just because it’s not an array doesn’t mean it doesn’t have a length. according to the DOM specs, a NamedNodeMap (the attribute list) does have a length*.

    * - from the specs:
    Could be. But some browsers simply don't take it at all as an array. . Maybe I was confused in my previous post. attributes should return a collection, right. A collection can be treated as an array, except from some array methods. The problem is that is some browsers it looks like attributes is implemented like an object, which make this property to lose its length.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,471
    Thanks
    13
    Thanked 361 Times in 357 Posts
    DOM list (i.e. NodeList & NamedNodeMap) are never and never have been arrays, that’s a common misconception. thus they don’t even have a single array method. and to the best of my knowledge, no browser has ever treated DOM lists as an array, they’re just too different. (e.g. DOM lists are live)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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