[Javascript] how to refer to (x)html elements

Hassan Schroeder hassan at webtuitive.com
Thu Nov 13 08:38:01 CST 2003


David Lovering wrote:

> The real problem with "id" in HTML (and correspondingly in JavaScript) is
> that it tries to do too many things.  

<non-standards-compliant example removed/>

Nonsense. ID does one thing -- it identifies a *single* element.

And if you ignore the standard, and it *seems* to work -- so what?
You ran with scissors and didn't poke your eye out, you went 65mph
in a 35 zone and didn't get a ticket. So what?

The standard is unambiguous. ID must be unique within a document.

As Roger points out, getElement*s*ByTagName, plural, returns an
array; getElementById, singular, does not. A quick search turns up
this good snippet on the DOM:

/* source: <http://xml.apache.org/xalan-j/apidocs/index.html> */

   org.w3c.dom
     Interface Document  extends org.w3c.dom.Node

  getElementById

     public Element getElementById(java.lang.String elementId)

   Returns the Element whose ID is given by elementId. If no such
   element exists, returns null. Behavior is not defined if more
   than one element has this ID. ...

   Parameters:
     elementId - The unique id value for an element.
   Returns:
     The matching element.
   Since:
     DOM Level 2

"Behavior is not defined" kinda sums it up, eh? :-)

FWIW!
-- 
Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.






More information about the Javascript mailing list