[snip]
The usage of name and id attributes is a little confusing to me. I've
simplified my task by avoiding duplicate names.
That's fair enough. I just thought I'd mention that it's not necessary.
As far as look-up goes, named DOM collections like forms and elements try
to find an element with a matching id, first. If one cannot be found, then
elements are checked for a matching name.
<input id="first">
<input name="first">
<input name="second">
formObj.elements['first'] // first INPUT
formObj.elements['second'] // third INPUT
The only way to access the second INPUT is either by adding a unique id,
or indexing by ordinal number.
Will this statement still work in IE with duplicate name and id
attribute names?
document.all[theId].style.visibility
Do you mean with
<input id="myInput" name="myInput">
Yes, it will.
There seems to be some overlap with the use if id and name. You seem to
code up the name tag in forms and the id tag everywhere else.
No, they're entirely separate. The name attribute is used in some non-form
control-related elements due to backwards compatibility. For example, the
name attribute serves no purpose on the FORM element itself, but NN4 won't
find that form if you reference it using an id. The same is true with IMG
elements.
With form controls, the name attribute is used during submission to pair
with the control's value. Only name can be used for this purpose, which is
why it's associated with controls so much. Whilst the name values can be
used to reference a form control for scripting purposes, it's just as easy
to use an id.
With non-form control-related elements, the decision to use a name or id
mainly comes down to what browser support you want. If you don't care for
NN4 and browsers of its generation, use id values to identify and
reference them. These elements could also then be used as target for
fragment identifiers (#myId) in links. If you do want NN4 support so you
can access elements from script, add an id and a name attribute with the
same value.
Did that help at all?
Does IE put both the id and name in the all collection? Any problems to
worry about if it does?
It does, but it becomes less consistent.
<input name="first">
<input id="first">
document.all['first']
returns a collection containing both INPUT elements. Rather than basing
the order on whether the element was matched by id or name, it's done by
document order.
var inputs = document.all['first']
inputs[0] // INPUT name="first"
inputs[1] // INPUT id="first"
This is why the code you've referenced below, and that in the FAQ (the
long code version), checks that IE is returning only by id, and that it
only returns one element.
I was trying to avoid potential problems suggested in an earlier post
that Mike Winter made:
Here's the code I use for gEBI and d.all support (part of a larger
collection of code):
...
It tries to ensure that only one element is returned by the all
collection, and that that element was retrieved by its id, only.
<
http://groups.google.com/groups?hl=en&lr=&selm=opsevovyvzx13kvk@atlantis>
I'd like to mention that there's an error in that code as pointed out by
Richard. See my response later in the thread for code that replaces the
document.all section.
Mike