...

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



codegoboom
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...

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

liorean
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?

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

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

that is called internally for the getElementById method.

liorean
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.

codegoboom
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum