473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

What's the difference between name and id?


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
Mar 8 '07 #1
10 17058
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 "getElementsByName(SomeName)" to return a
collection of elements.

wp.

Mar 8 '07 #2
VK
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["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?

Mar 8 '07 #3
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
Mar 8 '07 #4
On Mar 8, 8:23 pm, "VK" <schools_r...@yahoo.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.
--
Rob

Mar 8 '07 #5
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:

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
Mar 8 '07 #6
RC
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
Mar 8 '07 #7
Man-wai Chang <to***********@gmail.comwrote:
>
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
Sl********@bls.gov
http://members.cox.net/slatteryt
Mar 8 '07 #8
VK
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.
My bad!

Mar 8 '07 #9
Tim Slattery <Sl********@bls.govwrote:

>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
Sl********@bls.gov
http://members.cox.net/slatteryt
Mar 8 '07 #10
Thanks to everyone.

--
iTech Consulting Co., Ltd.
Expert of ePOS solutions
Website: http://www.itech.com.hk (IE only)
Tel: (852)2325 3883 Fax: (852)2325 8288
Mar 9 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
3
by: Johnny | last post by:
Hi, I wonder what is the difference between the built-in function getattr() and the normal call of a function of a class. Here is the details: getattr( object, name) Return the value of...
10
by: Johnny Lee | last post by:
Hi, I'm new in python and I was wondering what's the difference between the two code section below: (I) class TestResult: _pass_ = "pass" _fail_ = "fail" _exception_ = "exception"
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
2
by: nicholas | last post by:
Hi folks, I'm diving into XPathNavigator.SelectDescendants method. There's a boolean type parameter matchSelf, and MSDN says "To include the context node in the selection, true; otherwise,...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
1
by: tankbattle | last post by:
That is, what's the difference between <complexType name="Address" final="restriction"> <sequence> <element name="name" type="string"/> <element name="street" type="string"/> <element...
9
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.