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
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts

    element.getElementById instead of document.getElementById

    I want to restrict getElementById to search children of a specific element instead of searching the entire document, in the same way that I can do getElementsByTagName using a specific element as the parent.

    In this particular instance the parent is a table and the elements I'm interested in are all TDs, so I did the basic getElementsByTagName('TD') off the table and looped through this array checking the IDs. However, I'm suspecting that the browser can do getElementById faster than I can do a loop in javascript. Is there a neater way to do this? For now, I'll settle for IE-only solutions, though it would be nice to have things work in generic browsers.
    Wayne Christian

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Well, have a look at this post
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    wac
    wac is offline
    Regular Coder wac's Avatar
    Join Date
    Sep 2002
    Location
    Cary, North Carolina, USA
    Posts
    359
    Thanks
    2
    Thanked 0 Times in 0 Posts

    speed of associative arrays

    Yes,
    That's the basic gist of things. I was hoping that there was some way to do it without the javascript loop. I know how to write such a function, but I thought if there was a way for the browser to do it, this would surely be faster than a javascript loop.

    Here's another thing. I guess this should be a separate post.
    Suppose I have an associative array (an array with string index instead of numeric index) would that access be faster than looping through an integer indexed array. Here's my thought process....

    I create an associative array using the element ID as the index and the appropriate value (the element object), then each subsequent access would be
    myelem = myarray[id] ;
    where id is the string id of the element I want.
    Wayne Christian

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The browser has no built-in function for doing this. The one I posted there:
    Code:
    var
        oDiv=document.getElementById('elementId');
    
    function isDecendant(decendant,ancestor){
        return ((decendant.parentNode==ancestor)  ||
            (decendant.parentNode!=document) &&
                isDecendant(decendant.parentNode,ancestor));
    }
    
    if(oDiv && isDecendant(oDiv, oAncestor)){
        /*  do something with oDiv  */
    }
    ...is probably the fastest way of getting an element with a certain id only under the circumstances that it is a decendant of a certain other element. It will be in most cases be faster than iterating through the decendants of that element.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts
    I needed this functionality so i written this:

    Code:
    /**
     * Function will get element by id starting from specified node.
     * Author: Renato Bebić <renato.bebic@gmail.com>
     *
     */
    function GetElementById( dNode, id ) {
    
    	var dResult = null;
    
    	if ( dNode.getAttribute('id') == id )
    		return dNode;
    
    	for ( var i = 0; i < dNode.childNodes.length; i++ ) {
    		if ( dNode.childNodes[i].nodeType == 1 ) {
    			dResult = GetElementById( dNode.childNodes[i], id );
    			if ( dResult != null )
    				break;
    		}
    	}
    
    	return dResult;
    }
    I tried to make HTMLElement.prototype.getElementById but IE does not support prototyping on HTMLElement.
    Last edited by Renato Bebić; 09-03-2009 at 03:26 PM.


  •  

    Posting Permissions

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