Click to See Complete Forum and Search --> : id vs name which should be used?


kusi
11-12-2003, 04:59 AM
one can further reach the element via either document.getElementById('id_name') or document.all['id_name'] orrr document.layers['id_name']

What I perceive of this is that : name is for the form elements and id for the internal whole document. correct?

Charles
11-12-2003, 05:12 AM
Wrong. There are several versions of HTML and the older ones used "name" and the newer ones use "id". Note, that in HTML 4.0 it is incorrect to give the FORM element a "name". This is one of the reasons you ought to use HTML 4.01 and not HTML 4.0.

gil davis
11-12-2003, 06:39 AM
One very important difference between "id" and "name" is that an "id" must be unique whereas a "name" does not. You can use getElementsByName() which returns an array of objects with the same name, while getElementById() only returns one element.

kusi
11-12-2003, 07:14 AM
what about the "value" of these 'id' and 'name' ?
can they be the same?
and is 'id' accepted by all browsers?
at least NN4.7 and IE 5.5? +
opera:it accepts.

gil davis
11-12-2003, 07:40 AM
The values have to be the same. From W3C HTML 4.01 recommendation:
The id and name attributes share the same name space. This means that they cannot both define an anchor with the same name in the same document. It is permissible to use both attributes to specify an element's unique identifier for the following elements: A, APPLET, FORM, FRAME, IFRAME, IMG, and MAP. When both attributes are used on a single element, their values must be identical.

In NS 4, the ID attribute is valid for LAYER, ILAYER and OBJECT. However, any HTML tag with a style attribute "position: absolute" or "position: relative" becomes a layer. Many HTML tags support the NAME attribute, including form elements, anchors, images, frames, windows, embeds and applets.

The ID attribute was introduced along with CSS. Any browser that supports CSS will support ID in some way. The ID is useful for DHTML techniques as well as applying specific styles to one particular object.

DOM-compliant browsers (IE5+, NS6+, Mozilla) support ID for any HTML tag.

gil davis
11-12-2003, 07:47 AM
More from W3C:
Use id or name? Authors should consider the following issues when deciding whether to use id or name for an anchor name:

- The id attribute can act as more than just an anchor name (e.g., style sheet selector, processing identifier, etc.).
- Some older user agents don't support anchors created with the id attribute.
- The name attribute allows richer anchor names (with entities).
http://www.w3.org/TR/html4/struct/links.html#idx-anchor-8