We help IT Professionals succeed at work.

How does document.getElementById retreive references to HTML elements from the DOM? Tree Search? Hash Map?

Last Modified: 2012-08-13
I was just speaking with a coworker about this and neither of us knew. I've had no luck researching it online, so I'm opening it for discussion.

How does document.getElementById actually find the elements in the DOM? Is it a hash map, or does it recursively check each node's children, etc?

Watch Question

erikTsomikSystem Architect, CF programmer

it check the children node. it uses FIFO method. So the the first it finds that what is being used


No, you misunderstood I guess. I'm not asking what happens when you have two identical ids, I'm asking what the search algorithm is.
When the page is created, a DOM tree is also created, behind the scenes, if you will.  This tree contains all the tags in the page, and sets every attribute of those tags as child nodes.  I believe document.getElementById() reads the DOM tree as if it were XML.  It runs a FOR loop down the list of tags looking for a child node of ID, and when it finds one, it checks the value against the argument specified in calling the method.  As soon as it sees the matching id, it exits the loop and returns that tag as an object.

IE also searches for case insensitive matches and checks the "name" attribute as well. (IE6 and IE7 that is)

I have no idea what the actual algorithm is, but I have created similar methods in AJAX to search XML files the same way.

1.  Count the objects in the tree
2.  Create a for loop for the number of objects
3.  on each loop, read the value of childNode "id"
4.  if childNode "id" matches document.getElementById argument "id",
     return all childNodes of parentNode
     if not, return next loop
5.  if no childNode "id" matches document.getElementById argument "id",
     return null


So the answer is it recursively checks each node's children for the entire DOM tree? Your explanation is a little off, but it sounds like that's what you were saying.
Unlock this solution and get a sample of our free trial.
(No credit card required)
correction: "Each childNode CAN be a parentNode to other childNodes......"


It doesn't actually return the branch as you're thinking. Each node has three references to other nodes: "parentNode", "childNode", "nextSibling", "prevSibling(?)", which can be null if there is no respective node. The only reference that is returned by getElementById is the reference to the element with that id. You're probably right though, in how it crawls the tree.

Thanks for the input, made me think about some things in more depth than I have before.
Actually, it does return the entire node.  When you set a variable to getElementById(id), that variable becomes an object with it's own methods which can affect the original node.  For example you can say

var alert_box = document.getElementById('alert_div_1');

Now you can reference any of the childNodes simply by call that variable.  So if you want to set all the anchor tags in that div into an array, you can simply call:

var alert_box_anchors = alert_box.getElementsByTagName('a');

Which means they are all there.  Every child node is loaded into the object.  Another reason I say it's the entire branch, is that you can call those parentNodes, nextSibling, etc. directly from that object as well without referring back to your getElementById() method.  So you could call:

var alert_box_container_id = alert_box.parentNode.id.

If the javascript did not load the entire branch into the object, this would not be possible without recalling the original element every time.  The values themselves are not loaded into the object.  The object uses the tree and these methods to find the values.  So when you call for a value from that object contained within the variable, you are actually asking that object to grab the value from the DOM.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.