What is the difference between the id
and name
attributes? They both seem to serve the same purpose of providing an identifier.
I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.
Generally, it is assumed that name is always superseded by id. This is true, to some extent, but not for form fields and frame names, practically speaking. For example, with form elements the
name
attribute is used to determine the name-value pairs to be sent to a server-side program and should not be eliminated.Browsers do not use id in that manner
. To be on the safe side, you could use name and id attributes on form elements. So, we would write the following:To ensure compatibility, having matching name and id attribute values when both are defined is a good idea. However, be careful—some tags, particularly radio buttons, must have nonunique name values, but require unique id values. Once again, this should reference that id is not simply a replacement for name; they are different in purpose. Furthermore, do not discount the old-style approach, a deep look at modern libraries shows such syntax style used for performance and ease purposes at times. Your goal should always be in favor of compatibility.
Now in most elements, the name attribute has been deprecated in favor of the more ubiquitous id attribute. However, in some cases, particularly form fields (
<button>
,<input>
,<select>
, and<textarea>
), the name attribute lives on because it continues to be required to set the name-value pair for form submission. Also, we find that some elements, notably frames and links, may continue to use the name attribute because it is often useful for retrieving these elements by name.There is a clear distinction between id and name. Very often when name continues on, we can set the values the same. However, id must be unique, and name in some cases shouldn’t—think radio buttons. Sadly, the uniqueness of id values, while caught by markup validation, is not as consistent as it should be. CSS implementation in browsers will style objects that share an id value; thus, we may not catch markup or style errors that could affect our JavaScript until runtime.
This is taken from the book JavaScript-
The Complete Reference by Thomas-Powell
The
name
attribute is used when sending data in a form submission. Different controls respond differently. For example, you may have several radio buttons with differentid
attributes, but the samename
. When submitted, there is just the one value in the response - the radio button you selected.Of course, there's more to it than that, but it will definitely get you thinking in the right direction.
Here is a brief summary:
id
is used to identify the HTML element through the Document Object Model (via JavaScript or styled with CSS).id
is expected to be unique within the page.name
corresponds to the form element and identifies what is posted back to the server.ID tag - used by CSS, define a unique instance of a div, span or other elements. Appears within the Javascript DOM model, allowing you to access them with various function calls.
Name tag for fields - This is unique per form -- unless you are doing an array which you want to pass to PHP/server-side processing. You can access it via Javascript by name, but I think that it does not appear as a node in the DOM or some restrictions may apply (you cannot use .innerHTML, for example, if I recall correctly).
This link has answers to the same basic question, but basically, id is used for scripting identification and name is for server-side.
http://www.velocityreviews.com/forums/t115115-id-vs-name-attribute-for-html-controls.html