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 03-30-2005, 01:43 PM   PM User | #1
deadseasquirrel
Regular Coder

 
Join Date: Feb 2004
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
deadseasquirrel is an unknown quantity at this point
traversing a dom by anything other than tag name or ID

Is there any better way to traverse a DOM other than going by ID or tag name. I don't have IDs for certin things, but I do have a class associated with it, and traversing it by the tag is too encompassing.

Is there a good solution to this? I'm surprised there isn't mor functionality to traverse a DOM. IDs make the nodes very specific, and tags are too general, it seems like class is what you want to go by sometimes, but there is no function to do that. Can somebody let me know if I'm wrong on this matter?
deadseasquirrel is offline   Reply With Quote
Old 03-30-2005, 03:25 PM   PM User | #2
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
From getElementsByTagName you can create individual "getElementsByAttributeMatch" functions as you need them; for example, the class name test you mentiond:
Code:
document.getElementsByClassName = function(cname)
{
	var found = [];
	var eles = document.getElementsByTagName('*');
	var len = eles.length;
	for(var i=0; i<len; i++)
	{
		if(eles[i].className.indexOf(cname) != -1)
		{
			found.push(eles[i]);
		}
	}
	
	return found;
}
Using that basic approach, you can find and group elements by whatever relationship you want.

There are more efficient constructs - XPath for example, which can find groups of elements and attributes according to very complex rules, with only a single expression. Here's some more info http://www-jcsu.jesus.cam.ac.uk/~jg3...-tutorial.html but it's not very widely supported - moz only afaik.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

Last edited by brothercake; 03-30-2005 at 03:29 PM..
brothercake is offline   Reply With Quote
Old 03-31-2005, 08:08 AM   PM User | #3
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
Even so, brothercake, the code will traverse all the elements by same tag name, so that is will not solve the deadseasquirrel problem (in fact not entirely).

deadseasquirrel, there is no way to avoid traversing elements by tag name as there is no specific way to refere the elements other way than 1. by id 2. by tag name 3. by name.

The advantage of brothercake's code is that the traverse is made only once (preferable onload). Now for future repeatable methods, the so selected elements are already set in a restrictive array.

Well, you may also combine and isolate the elements using compunded reference such as:

document.getElementById(id).getElementsByTagName(tag)
which will set a restrictive collection as well (merely used in table/rows/cells manipulation), sometimes even without using any id, something like

document.getElementsByTagName('table')[index].getElementsByTagName('tr')[index].getElementsByTagName('td')[index]
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 03-31-2005 at 08:12 AM..
Kor is offline   Reply With Quote
Old 03-31-2005, 01:02 PM   PM User | #4
deadseasquirrel
Regular Coder

 
Join Date: Feb 2004
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
deadseasquirrel is an unknown quantity at this point
Ok, thanks for that confirmation. I wish I was using more JSP or .Net stuff, I bet there are DOM APIs that are more advanced to make things easier.
deadseasquirrel is offline   Reply With Quote
Old 03-31-2005, 09:57 PM   PM User | #5
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
I don't get your point - what's not easy? What sort of constructs or methods are you looking for?
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 04-01-2005, 07:36 AM   PM User | #6
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
I guess he would like something like
getElementsBySomeAttribute

He seems not to sense that even if would have been such methods, all the elements are to be traversed anyway, thus the DOM:

if(root.getElementsByTagName(tag)[i].getAttribute('someAttribute')!=null)

will do exactly the same thing in the same time.

Well, maybe he thinks there are too many characters in simple code line... Well, probably he's right... But DOM has well structured cores so far...

So that: this is it, this is the syntax...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-01-2005, 01:09 PM   PM User | #7
deadseasquirrel
Regular Coder

 
Join Date: Feb 2004
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
deadseasquirrel is an unknown quantity at this point
Well one feature of the DOM model that I think would be good is if it didn't give you all the sub-children nodes.

If I do a [parentNode].getElementsByTagName("div") I don't necessarily want all the grandchildren, I think there should be a way of getting only the children nodes. That's one feature that I think is very reasonable. Yes of course there are ways of getting around that, I can build my own function to loop from the first child and do a <div> tag name search. I could program a lot of low level functions, but yes I'm surprised functionality like what I described doesn't exist within the DOM model.
deadseasquirrel is offline   Reply With Quote
Old 04-01-2005, 06:33 PM   PM User | #8
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
Oh I see, you'd rather have those methods already available, because that would be faster than iterating through larger collections yourself. That's fair enough, but there aren't many scripts where it's gonna make much of a difference overall.

The DOM is a standardised interface across languages and markup syntaxes, and iirc ID and tagname are the only constants - XML doesn't have any other attributes that all elements can have by default (apart from xmlns, but we have a method for that, in the form of getElementsByTagNameNS)

I might be wrong on those points, but afaik that's the situation.
__________________
"Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark
brothercake is offline   Reply With Quote
Old 04-01-2005, 06:48 PM   PM User | #9
deadseasquirrel
Regular Coder

 
Join Date: Feb 2004
Posts: 192
Thanks: 0
Thanked 0 Times in 0 Posts
deadseasquirrel is an unknown quantity at this point
oh, that's one thing that I've learned from this thread - so that's what getElementsByTagNameNS is all about and xmlns. I've been meaning to read more about that. Thanks.
deadseasquirrel 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 04:55 PM.


Advertisement
Log in to turn off these ads.