What's the difference between name and id?

W

wisestpotato

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 "getElementsByName(SomeName)" to return a
collection of elements.

wp.
 
V

VK

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["FirstControl"];
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:

<input type="radio" value="yes" name="rdg" id="rd01">
<label for="rd01">Yes</label>

<input type="radio" value="no" name="rdg" id="rd02">
<label for="rd02">No</label>


For anchors and links collections name attribute usage is deprecated
and rarely used.

document.images["ImageName"] is still pretty much in use.

Both from above are from the same DOM 0 model.

P.S.
Do you have a Gecko-only version to look at?
 
M

Man-wai Chang

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 "getElementsByName(SomeName)" to return a
collection of elements.

What would happen if the HTML has a duplicated id? Will
getElementByName("duplicated_id") fail or just return the first one it
found?

--
.~. Might, Courage, Vision, SINCERITY. http://www.linux-sxs.org
/ v \ Simplicity is Beauty! May the Force and Farce be with you!
/( _ )\ (Ubuntu 6.10) Linux 2.6.20.1
^ ^ 20:59:01 up 11 days 4:40 0 users load average: 1.05 1.01 1.00
news://news.3home.net news://news.hkpcug.org news://news.newsgroup.com.hk
 
R

RobG

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.
 
R

RobG

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:

news:comp.infosystems.www.authoring.html
<URL: http://groups.google.com/group/comp.infosystems.www.authoring.html
You might also like to look at the HTML specification for all the
elements that can have a name attribute and what it means for each:

Expert of ePOS solutions

As J said in Men In Black:

"Unlimited technology from the whole universe, and
we're crusin' around in a Ford P-O-S."

:)
 
R

RC

Man-wai Chang said:
Why 2 reference points?

Just think in real life.
Two different people may be have the
same name, but their driver license number (ID)
are not the same.

In a file you can not have duplicate ID numbers.

getElementById(SomeId)

should fail or only get the first one
or last one
 
T

Tim Slattery

Man-wai Chang said:
Why 2 reference points?

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.getElementByName() returns an array, since it assumes
that there may be several elements with the same name. But
document.getElementById() 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.
 
V

VK

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.

My bad!
 
T

Tim Slattery

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.

I meant "in a PAGE", of course. Arghhh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top