...

View Full Version : traversing a dom by anything other than tag name or ID



deadseasquirrel
03-30-2005, 02:43 PM
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?

brothercake
03-30-2005, 04:25 PM
From getElementsByTagName you can create individual "getElementsByAttributeMatch" functions as you need them; for example, the class name test you mentiond:


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/~jg307/mozilla/xpath-tutorial.html but it's not very widely supported - moz only afaik.

Kor
03-31-2005, 09:08 AM
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]

deadseasquirrel
03-31-2005, 02:02 PM
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.

brothercake
03-31-2005, 10:57 PM
I don't get your point - what's not easy? What sort of constructs or methods are you looking for?

Kor
04-01-2005, 08:36 AM
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...
;)

deadseasquirrel
04-01-2005, 02:09 PM
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.

brothercake
04-01-2005, 07:33 PM
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.

deadseasquirrel
04-01-2005, 07:48 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum