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

David Lovering dlovering at gazos.com
Wed Nov 12 14:34:53 CST 2003


Yes, you can have multiple uses of an id -- but it makes searching the DOM
tree for it a bit more complicated, as you have to navigate from parent to
child (ad infinitum) until you get to the right instantiation of the id you
are looking for.  Generally, I try to keep my ids unique (except perhaps for
radio buttons, which already share a group id anyway).  I guess it boils
down to personal programming preferences.  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 -- the first one?  the next one in line?
the last one?  Who knows!  I certainly don't.

I defer to Chris' superior wisdom on the portability of getElementById to
various browsers.  I do know that getElementsByTagName works virtually
everywhere, (at least where I've tested it), but some developer BBS's have
had warnings about the former method.  I for one would be delighted if it
worked seamlessly everywhere.

On the XML side of things, in theory the HTML object ids should apply
perfectly and exactly to XML, and vice-versa.  In Opera I had some cases
where the mapping was one-to-one but not necessarily onto, and I guess that
was what I was thinking of when I made my remark.  [Possibly the issue of
non-unique id's in HTML impairs their portability into the more OOL-ish XML
name-space; I wouldn't know].

Thanks again to Chris for helping to clarify my earlier vague comments.

-- Dave Lovering


----- Original Message ----- 
From: "Chris Tifer" <christ at saeweb.com>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Wednesday, November 12, 2003 11:29 AM
Subject: Re: [Javascript] how to refer to (x)html elements


>
> ----- Original Message ----- 
> From: "David Lovering" <dlovering at gazos.com>
> To: "[JavaScript List]" <javascript at LaTech.edu>
> Sent: Wednesday, November 12, 2003 10:18 AM
> Subject: Re: [Javascript] how to refer to (x)html elements
>
>
> > Yes, but there are a few caveats.
> >
> >   var myForm = document.getElementById("form_example");
> >   var myObject = myForm.getElementById("input_example");
> >
> > BTW -- you can't reuse an id like "example" for both an input object and
a
> > form, and expect things to work OK.
>
> What do you mean by that?  Are you stating IDs should be unique?
>
> > The caveats are that not every browser will permit you to do this (the
> > getElementById call was defined originally for IE)
>
> If this person is developing with advanced browsers in mind, there should
be
> absolutely no problems. getElementID is now implemented in all major
> browsers as far as I know.
>
> > , and secondly, the HTML
> > object id may not necessarily map to the XML id.  I've had some problems
> > with this myself, and the XML
> > id only works flawlessly when it is defined within an XML construct.
> > However, it SHOULD work -- providing that the object is "dual-homed" and
> is
> > identified with both an XML and HTML label.
> >
>
> What do you mean about an object id not mapping to an XML id?
>
> Chris Tifer
> http://emailajoke.com
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
>
>





More information about the Javascript mailing list