On 8 Mar, 08:55, Man-wai Chang <toylet.toy...@ gmail.comwrote:
Why 2 reference points?
>
Id should be unique. That is, an id should apply to only one element
within a page. Lots of elements can share the same "name" however.
This is why the DOM gives you "getElementById (SomeId)" to return a
single element, and "getElementsByN ame(SomeName)" to return a
collection of elements.
On Mar 8, 11:55 am, Man-wai Chang <toylet.toy...@ gmail.comwrote:
What's the difference between name and id?
Why 2 reference points?
name is coming from the initial DOM 0 (DOM Zero Level) interface
model. In this model form controls are identified by name or by index
in elements collection:
document.forms[0].elements[0];
document.forms[0].elements["FirstContr ol"];
Only controls having name attribute set are being submitted, this rule
is in effect since then and forever.
id attribute was introduced only in DOM 1. As other poster pointed out
ID supposed to be unique for the given document, while name can be the
same for many elements (think of radio buttons group for instance).
This is why say accesskey is attached to ID and not to name. That is
not the only neither the main reason but it gives the idea why
sometimes it is useful to set both id and name for a form control,
like:
Id should be unique. That is, an id should apply to only one element
within a page. Lots of elements can share the same "name" however.
This is why the DOM gives you "getElementById (SomeId)" to return a
single element, and "getElementsByN ame(SomeName)" to return a
collection of elements.
What would happen if the HTML has a duplicated id? Will
getElementByNam e("duplicated_i d") fail or just return the first one it
found?
On Mar 8, 8:23 pm, "VK" <schools_r...@y ahoo.comwrote:
[...]
id attribute was introduced only in DOM 1.
"Only"? DOM 0 didn't exist until there was a DOM 1.
For anchors and links collections name attribute usage is deprecated
and rarely used.
Wrong. The name attribute is not deprecated for A elements (which can
be either anchors or links) in HTML 4. An anchor is *defined* as an A
element with a name - if the name attribute for A elements is
deprecated, then so are anchors.
Both from above are from the same DOM 0 model.
They are from the same HTML specification, they are incidental to
javascript.
On Mar 8, 6:55 pm, Man-wai Chang <toylet.toy...@ gmail.comwrote:
Why 2 reference points?
Oh, you mean why both id and name? This is a javascript forum, where
you might get a good answer but it is not this group's speciality.
Name and ID belong to HTML, so ask in a group dedicated to that topic:
IMHO, "name" was intended to be used to identify form fields. When a
form is submitted the "name" attribute is used to identify each item
of data transmitted. Since there can be more than one form on a page,
a "name" does not have to be unique within a page. Moreover, multiple
fields - in particular radioboxes - will have the same name attribute
in the same form.
The ID attribute is intended to uniquely identify an element - ANY
element, whether part of a form, or a paragraph, span, list item, div,
anything at all - in a form. The expectation is that values of id will
be unique within a form.
So...document.g etElementByName () returns an array, since it assumes
that there may be several elements with the same name. But
document.getEle mentById() returns a single value, since it assumes
that ID will be unique. If there is more than one element with the
requested ID, the function returns the first one.
On Mar 8, 4:23 pm, "RobG" <r...@iinet.net .auwrote:
For anchors and links collections name attribute usage is deprecated
and rarely used.
>
Wrong. The name attribute is not deprecated for A elements (which can
be either anchors or links) in HTML 4. An anchor is *defined* as an A
element with a name - if the name attribute for A elements is
deprecated, then so are anchors.
>The ID attribute is intended to uniquely identify an element - ANY
>element, whether part of a form, or a paragraph, span, list item, div,
>anything at all - in a form.
Comment