name or id

Posted on 2003-03-03
Medium Priority
Last Modified: 2013-11-19
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?
Question by:russ4ell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8057468
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"/>
LVL 14

Accepted Solution

avner earned 200 total points
ID: 8057487
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.

Expert Comment

ID: 8059712
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.
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.


Expert Comment

ID: 8062622
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.  
LVL 53

Expert Comment

ID: 8820475
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:


LVL 53

Expert Comment

ID: 9012160
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question