473,394 Members | 1,481 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,394 software developers and data experts.

Difference between name= and id=

I'm working on a JavaScript program and I'm running into name=blah and
id=blah. Sometimes it's one or the other and other times it
both. Does anyone have a good reference that can help me figure this
out? The books I have don't seem to really be of any help as they're
more towards JavaScript coding that working with the html/javascript.

Here are a few examples:

<frameset cols="600, *" id="overallFrame">
<frameset id="leftSide" rows="*, 0">
<frame name="L" src="L.html">
:
:
--
Linux Home Automation Neil Cherry nc*****@comcast.net
http://home.comcast.net/~ncherry/ (Text only)
http://hcs.sourceforge.net/ (HCS II)
http://linuxha.blogspot.com/ My HA Blog
Jul 23 '05 #1
6 23841
Neil Cherry wrote:
I'm working on a JavaScript program and I'm running into name=blah and
id=blah. Sometimes it's one or the other and other times it
both. Does anyone have a good reference that can help me figure this
out? The books I have don't seem to really be of any help as they're
more towards JavaScript coding that working with the html/javascript.


The definitive "guide" (the W3C HTML 4.01 spec) is here:

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

You may want to also look at xhtml, but for now HTML 4.01 is it.

To speed your resolution of name/id, the full list of attributes and
the elements they apply to is here:

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

Now my understanding is that:

When Netscape created JavaScript, they used "name" to identify
elements. MS built IE to copy Netscape, so also supported "name".

However, the HTML spec decided to use id to identify elements but keep
name for backwards compatibility. Name still applies to form elements
and lots of others (e.g. name is mandatory on PARAM elements). MS seem
to hate ever removing functionality, so they continue to support name
as if it was ID.

Now you're really confused, right?

Many programmers put the same name and ID on form elements. This isn't
required, but may be helpful if you intend to reference the element
using both the forms.elements collection and getElementById.

e.g. <form name="aForm" ... >
<input name="aa" id="aa" ... >

Can be referenced using the name as:

document.forms['aForm'].elements['aa']

or the id as:

document.getElementById('aa')

You can put the same name on a number of form elements to create a
collection - e.g. radio buttons - but use with care.

The only reason to give any element an id is if you intend to reference
if in some way, otherwise it is unnecessary. The name of form elements
is included in the data sent when the form is submitted, the id isn't.

If you use document.getElementById('blah') in IE, it will match
<... name="blah"> as well as <... id="blah">. Other browsers will only
match id.

So the bottom line is:

Form elements must have a name if you want them to be submitted
(put a name on all of them unless you are certain you want to do
otherwise - e.g. buttons)

Some element *must* have a name.

Use id only if you need to refer to an element (usually using
getElementById).

If an element needs both an ID and a name, it's probably convenient to
make them the same.

Keep names and ids of different elements unique (mandatory for IDs).
--
Rob
Jul 23 '05 #2
"RobG" <rg***@iinet.net.auau> wrote in message
news:41*********************@per-qv1-newsreader-01.iinet.net.au...
Neil Cherry wrote:
I'm working on a JavaScript program and I'm running into name=blah and
id=blah. Sometimes it's one or the other and other times it
both. Does anyone have a good reference that can help me figure this
out? The books I have don't seem to really be of any help as they're
more towards JavaScript coding that working with the html/javascript.
The definitive "guide" (the W3C HTML 4.01 spec) is here:

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

You may want to also look at xhtml, but for now HTML 4.01 is it.

To speed your resolution of name/id, the full list of attributes and
the elements they apply to is here:

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

<snipped/>
So the bottom line is:

Form elements must have a name if you want them to be submitted
(put a name on all of them unless you are certain you want to do
otherwise - e.g. buttons)

Some element *must* have a name.

Use id only if you need to refer to an element (usually using
getElementById).

If an element needs both an ID and a name, it's probably convenient to
make them the same.

Keep names and ids of different elements unique (mandatory for IDs).


In addidion to that, ID's are used by CSS when containing a style
declared with an id selector (#):

The id selector is different from the class selector(.).
While a class selector may apply to SEVERAL elements on a page,
an id selector always applies to only ONE element.
An ID attribute must be unique within the document.
The style rule below will match a p element that has the id value "para1":
p#para1
{
text-align: center;
color: red
}

--
Dag.
Jul 23 '05 #3
On Sat, 15 Jan 2005 16:31:23 +1000, RobG wrote:
Neil Cherry wrote:
I'm working on a JavaScript program and I'm running into name=blah and
id=blah.
The definitive "guide" (the W3C HTML 4.01 spec) is here:

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

You may want to also look at xhtml, but for now HTML 4.01 is it.


Rob and Dag, thank you very much this is very useful!

--
Linux Home Automation Neil Cherry nc*****@comcast.net
http://home.comcast.net/~ncherry/ (Text only)
http://hcs.sourceforge.net/ (HCS II)
http://linuxha.blogspot.com/ My HA Blog
Jul 23 '05 #4
In article <sl****************@wolfgang.uucp>,
Neil Cherry <nj*@wolfgang.uucp> wrote:
I'm working on a JavaScript program and I'm running into name=blah and
id=blah. Sometimes it's one or the other and other times it
both. Does anyone have a good reference that can help me figure this
out? The books I have don't seem to really be of any help as they're
more towards JavaScript coding that working with the html/javascript.


The XHTML spec says:

"Note that in XHTML 1.0, the name attribute of these elements is
formally deprecated, and will be removed in a subsequent version of
XHTML."

<http://www.w3.org/TR/xhtml1/#h-4.10>

Use 'id'.

--
David Phillip Oster
Jul 23 '05 #5
David Phillip Oster wrote:
In article <sl****************@wolfgang.uucp>,
Neil Cherry <nj*@wolfgang.uucp> wrote:

I'm working on a JavaScript program and I'm running into name=blah and
id=blah. Sometimes it's one or the other and other times it
both. Does anyone have a good reference that can help me figure this
out? The books I have don't seem to really be of any help as they're
more towards JavaScript coding that working with the html/javascript.

The XHTML spec says:

"Note that in XHTML 1.0, the name attribute of these elements is
formally deprecated, and will be removed in a subsequent version of
XHTML."

<http://www.w3.org/TR/xhtml1/#h-4.10>

Use 'id'.


In regard to HTML 4, the name *must* be used for form controls
to be successful. If they only have an id, they will not be
submitted.

"When a form is submitted for processing, some controls have
their name paired with their current value and these pairs
are submitted with the form. Those controls for which
name/value pairs are submitted are called successful
controls."

<URL:http://www.w3.org/TR/html4/interact/forms.html#control-name>

--
Rob
Jul 23 '05 #6
RobG wrote:

[Stuff about name in XHTML]
In regard to HTML 4, the name *must* be used for form controls
to be successful.


That remains true for XHTML as well.

Only form controls, OBJECTs, PARAMs, and METAs are permitted a name
attribute.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

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

Similar topics

4
by: Nimmi Srivastav | last post by:
Once and for all can someone kindly tell me the difference between C and C++ linkage. I thought I understood it till someone showed me the other day that C functions, that would ordinarily require...
3
by: John Harman | last post by:
Hi, I'm trying to do a MySQL Query using Mysql 3.23.58 something like that below SELECT name FROM customers WHERE name LIKE "Fred" ORDER BY difference(name,"Fred"); The difference piece...
0
by: wooks | last post by:
<?xml version="1.0" ?> - <xsd:schema targetNamespace="urn:faster:userlogin" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:faster:userlogin"> <xsd:include...
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...
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,...
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...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
2
by: manishamca | last post by:
can anyone say me how to find the difference of values present in two textbox and display the result in the third textbox. for eg: <input type=text name=t1 value=123> <input...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.