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

David Lovering dlovering at gazos.com
Wed Nov 12 23:16:59 CST 2003


Well... sort of.

The real problem with "id" in HTML (and correspondingly in JavaScript) is
that it tries to do too many things.  For example, one can define a CSS
style block thusly:

<style TYPE='text/css'>
<!--
    #normaltext { font-family: arial,helvetica,sans-serif; font-size: 8pt;
vertical-align: middle; text-indent: 0.4em; border-style: solid;
border-width: 1pt }
// -->
</style>

... and then later inside a form define an input text field referencing the
id defined in the CSS block

<form name='myForm' id='myForm'>
  <center><input type='text' name='myText' id='normaltext' size=30
value='something'></center>
</form>
...

and voila! your text input has all the 'correct' style features.  Moreover,
you can use the same id for 200 different text input fields (with different
names of course), and everything will work out perfectly OK.

Of course, this defeats the REAL purpose of "id" (as a DOM identifier), and
is more wretched than I can possibly begin to explain in the space here.
However, it IS allowed (and is in fact used on some of the Microsoft
FrontPage macros), and does work -- at least in IE and some of the more
IE-like browsers floating around out there.  Mind you, I really wouldn't
suggest everyone rush out to do things this way (shudder!) but it does
rather put the kobosh on the absolute "NO" that Hassan gave my earlier
remark.

Generally, there's enough complexity in this game that any answer, whether
it is "Yes" or "No" should be qualified, and rendered into a "Maybe" or a
"Sometimes".    In this case I think the problem was caused merely by
Microsoft messing about with something they didn't invent thereby rendering
it into a construct whose new function did not match its original intention.
[Where have we seen this before?]  Also, many things which are doable are
still bad programming practice, and should be discouraged on those grounds
alone.

Until the world works perfectly (as both Hassan and I would prefer), one
needs to be aware of these odd little quirks.

-- Dave Lovering


----- Original Message ----- 
From: "Hassan Schroeder" <hassan at webtuitive.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Wednesday, November 12, 2003 3:36 PM
Subject: Re: [Javascript] how to refer to (x)html elements


> David Lovering wrote:
>
> > Yes, you can have multiple uses of an id
>
> No. Flat no. That's wrong, in both HTML 4 and XHTML 1 --
>
> HTML 4
> source: <http://www.w3.org/TR/html4/struct/global.html#adef-id>
>
> id = name [CS]
>      This attribute assigns a name to an element. This name must be unique
in a
> document.
>
>
> XHTML 1
> source: <http://www.w3.org/TR/xhtml1/#normative>
>
> 4.10. The elements with 'id' and 'name' attributes
>
> HTML 4 defined the name attribute for the elements a, applet, form, frame,
> iframe, img, and map. HTML 4 also introduced the id attribute. Both of
these
> attributes are designed to be used as fragment identifiers.
>
> In XML, fragment identifiers are of type ID, and there can only be a
single
> attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute
is
> defined to be of type ID.
>
> > Also, I'm not sure what the
> > convention is for returning pointers to a getElementById() call when
there
> > are multiple objects of the same id
>
> There is no "convention" because it's wrong. Did I mention that? :-)
>
> -- 
> Hassan Schroeder ----------------------------- hassan at webtuitive.com
> Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com
>
>                            dream.  code.
>
>
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>





More information about the Javascript mailing list