Can somebody please explain to me the internal implementation of HTML getElementById() method ? Is it traversing whole DOM tree to find the specified element or is it intelligen
It depends on the browser, but most probably use a hash map from id->element. It's true that there are many invalid pages that have duplicate ids. However, the browser will still only return one element, not a collection.
I don't know what you mean by "near by elements" since the method only exists on document
.
If you're interested, you can find the implementation for free software browsers such as Firefox and Chrome.