Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

name or id

When coding html should I be using the NAME or the ID attribute.  I've read that in XHTML that NAME is being depreciated, so I've been using ID.  However, it is a lot easier and faster (use d.myname instead of d.getElementById('myname')) accessing the NAME attribute in Javascript.  Also for CGI I seem to be unable to access parameters without a NAME attribute.  This is the first bit of programming I've really been doing and I want to have a good coding practice

So what are your views, please?
1 Solution
CGI only supports "name", so you have to use "name" for form elements.
With HTML elements, which are formatted by CSS and/or accessed via JavaScript, you shouldt use "id", because the next browser generation may not support "name".
If your page has to be compatible to NN4.x or IE3.x, you have to use both attributes:
<SomeElement name="abc" id="abc"/>
If NAME will be deprecated, it's not going to be too soon.
As you noticed, NAME is mandatory if you want to interact with the Server (PERL, ASP, etc..) When working on a page, While ID is unique (and the DOM does not support multipale IDs) the NAME can be used more then once (DOM supports document.getElementsByName("AAA")) but.. this will work for FORM elements only, that is :

Bottom line, Use both in Form elements, otherwise use ID only.
Just set your xhtml page to be transitional so you don't bump into any problems with having name there, as it isn't part of the strict document definition.

But I WOULD use document.getElementById("whatever") - because the W3C DOM methods are the most cross-browser compatible.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

I use the "id" attribute when I want sure to access an unique element. "name" attribute help me to access an array of elements from a page (b.t.w. a page can have only one element with one id and can have more elements with same "name"s). Another comment about "name" is that you can access elements by form (ex. document.myform.name_of_element). In most cases when I want to access an element I use the "id" attribute - it's more sure and works in all browsers.  
This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

If the user does not know how to close the question, the options are here:


It is time to clean this abandoned question up.

I am putting it on a clean up list for CS.

points to avner -- grade A


If anyone participating in the Q disagrees with the recommendation,
please leave a comment for the mods.



Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now