Difference between id and name attributes in HTML
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
id and name may both look like simple labels on an HTML element, but they serve different purposes. The short version is that id identifies one specific element in the document, while name is mainly used for grouping or submitting form-related values.
What id Means
An id value is meant to be unique within the page. It gives one element a stable identity that CSS, JavaScript, labels, and fragment links can refer to.
That id can then be used in several ways:
Because id is supposed to be unique, document.getElementById("pricing") returns one specific element.
What name Means
The name attribute is different. It is primarily used to give form controls a key for form submission, and multiple elements are allowed to share the same name.
When the form is submitted, the browser sends the control value under the key q. Without name, many form controls do not contribute any value to the submitted form data at all.
Shared name values are also how radio buttons form a group:
All three inputs belong to the same logical field because they share the same name.
They Are Not Interchangeable
One common mistake is treating name as an older version of id or assuming either one can always replace the other. They solve different problems.
For example, a label's for attribute points to an element id, not its name:
Here:
- '
id="email"connects the label to the input and gives the element a unique DOM identity' - '
name="email"determines the submitted field key'
In many real forms, an input has both attributes because both jobs are needed.
JavaScript and CSS Differences
id has special first-class support in browser APIs and selectors. document.getElementById(...) is built around it, and CSS has the dedicated #idValue selector.
name does not have the same uniqueness guarantee. JavaScript can still query it, but the semantics are different:
This may return multiple elements, which is expected for radio groups and repeated form inputs.
CSS can target name, but only through a normal attribute selector:
That works, but name is not a replacement for a document-unique identifier.
When to Use Each One
Use id when you need:
- a unique DOM target
- a fragment link such as
#section-one - a label association through
for - a stable hook for scripting or accessibility relationships
Use name when you need:
- form data to be submitted under a specific key
- a group of radio buttons to act as one field
- multiple related inputs to share the same logical field name
Many inputs legitimately use both attributes because they participate in both the DOM and form submission.
Common Pitfalls
- Reusing the same
idon several elements, which breaks uniqueness assumptions. - Omitting
nameon a form control and then wondering why its value is not submitted. - Using
namewhen a label or script actually needsid. - Assuming
idvalues are sent automatically with form submissions. - Treating
nameas a unique identifier even though it is often intentionally shared.
Summary
- '
iduniquely identifies one element in the document.' - '
namemainly gives form controls their submission key or grouping name.' - '
idis used by fragment links, labels, CSS#...selectors, andgetElementById.' - '
nameis important for submitted form data and radio button grouping.' - Many form elements use both because the two attributes solve different problems.

