Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2011, 06:47 AM   PM User | #1
CX_LPS
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
CX_LPS is an unknown quantity at this point
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.
CX_LPS is offline   Reply With Quote
Old 04-15-2011, 09:13 AM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-15-2011, 02:00 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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:
Quote:
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 04-15-2011, 02:19 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-15-2011, 07:17 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:50 PM.


Advertisement
Log in to turn off these ads.