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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by brothercake; 03-30-2005 at 04:29 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    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
    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]
    Last edited by Kor; 03-31-2005 at 09:12 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    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
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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