View Full Version : how does getElementsByTagName("*")['id'] work?

08-27-2004, 05:17 AM
Does the interpreter loop through and compare all id's to find that member, or what?

<button id="foo" onclick="alert(document.getElementsByTagName('*')['foo']==this?'yes':'no')">Am I foo?</button>

...maybe not, because document.getElementsByTagName('*')[this] doesn't do the same...

08-27-2004, 05:34 AM
Or maybe it's more like an object property... DOMelementsWithId={foo:[OBJECT-button#0], blah:[OBJECT-div#2]}...

08-27-2004, 05:53 AM
Hmm, that might work, but it's not anywhere in the DOM specs. So, you really shouldn't do it like that. getElementsByTagName returns a NodeList, and NodeLists only have index access, not key access. (Though implementations are free to extend as long as they don't change the behavior of anything from the spec.)

IDs must be unique for a document anyway, so why not simply use getElementById?

08-27-2004, 06:23 AM
Hmm, another mystery... I wonder if that's at all related to NamedNodeMap...

08-27-2004, 07:43 AM
or maybe... :D

that is called internally for the getElementById method.

08-27-2004, 03:00 PM
I would think it's written in such a way that it mimics the IE document.all vector, despite not being an exact replica of it according to the DOM specs.

08-27-2004, 04:01 PM
...and a more educated guess, at that...

So why use it?
For no good reason, really; though if you had to find an element by either unique-id or unique-name (but didn't know which ahead of time), that could work for both.