Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > What's the difference between name and id?

Reply
Thread Tools

What's the difference between name and id?

 
 
Man-wai Chang
Guest
Posts: n/a
 
      03-08-2007

Why 2 reference points?

--
iTech Consulting Co., Ltd.
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
 
Reply With Quote
 
 
 
 
wisestpotato
Guest
Posts: n/a
 
      03-08-2007
On 8 Mar, 08:55, Man-wai Chang <toylet.toy...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      03-08-2007
On Mar 8, 11:55 am, Man-wai Chang <toylet.toy...@gmail.com> wrote:
> 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.
> Website:http://www.itech.com.hk(IE only)

Do you have a Gecko-only version to look at?



 
Reply With Quote
 
Man-wai Chang
Guest
Posts: n/a
 
      03-08-2007
> 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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-08-2007
On Mar 8, 8:23 pm, "VK" <schools_r...@yahoo.com> wrote:
[...]
> 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.


--
Rob

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-08-2007
On Mar 8, 6:55 pm, Man-wai Chang <toylet.toy...@gmail.com> wrote:
> 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....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:

<URL: http://www.w3.org/TR/html4/index/attributes.html >


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



--
Rob


 
Reply With Quote
 
RC
Guest
Posts: n/a
 
      03-08-2007
Man-wai Chang wrote:
>
> 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
 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      03-08-2007
Man-wai Chang <> wrote:

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

--
Tim Slattery

http://members.cox.net/slatteryt
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      03-08-2007
On Mar 8, 4:23 pm, "RobG" <r...@iinet.net.au> wrote:
> > 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.


My bad!

 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      03-08-2007
Tim Slattery <> wrote:


>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!

--
Tim Slattery

http://members.cox.net/slatteryt
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 7.17 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? PerlFAQ Server Perl Misc 0 01-06-2011 05:00 PM
[pointers and arrays]: The difference between an array name and a pointer iamchiaweilin@gmail.com C Programming 7 10-02-2006 11:03 PM
Difference between System.Web.HttpContext.Current.User.Identity.Name and System.Threading.Thread.CurrentPrincipal.Identity.Name jeremy.rice@alkermes.com ASP .Net Security 5 11-08-2005 05:25 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57