👀 Live Preview
Type in fields and see how name becomes the submission key:
Would submit: email=user@example.com&message=Hello

The name attribute assigns the submission key for a form control. When a user submits a form, the browser sends data as name=value pairs—for example username=alex and email=alex@example.com. Without a name, the control is omitted from the submission. Use it on <input>, <textarea>, <select>, and <button>. Do not confuse name with id: id labels the element in the page; name labels the field for the server.
Submit label.
Server vs DOM.
Same name.
Shared or unique.
Not submitted.
Rename field.
name AttributeThe primary purpose of name is to identify form data on submission. Server-side code reads keys like password or quantity from the request. JavaScript can read the same data with FormData or new URLSearchParams(new FormData(form)).
Radio buttons in one group intentionally share a name so the browser submits only the selected option’s value. Checkboxes can share a name (multiple values) or use distinct names. Multi-select lists send several values under one name when multiple is set.
The old reference mentioned “link anchors.” Using name on <a> for in-page links is obsolete. Use id on the target and href="#section-id" instead.
Add name with a string identifier on form controls:
<form action="/signup" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<label><input type="radio" name="plan" value="free"> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
<button type="submit">Sign up</button>
</form>name="email" or name="user[first]" for nested keys in some frameworks.name, different value on each radio.id is for document identity and <label for>; name is for submitted data.inputElement.name = "newKey".The name attribute accepts any string that is a valid form field name:
name="username" — Simple single field key.name="email" — Common contact form field.name="color" on radios — Group name; value comes from selected radio’s value.name="tags" on multi-select — Multiple values submitted with same key.name="items[]" — Convention used by some backends for arrays (framework-specific).document.getElementById("dynamicField").name = "feedback";
document.querySelector("input").setAttribute("name", "comment");| Use case | Markup | Notes |
|---|---|---|
| Text field | <input name="username"> | Submitted as username=… |
| Radio group | name="size" value="m" | Same name on each radio |
| Hidden field | <input type="hidden" name="token"> | Still needs name |
| Label pairing | id + for | Not name (use id) |
| JS rename | el.name = "newKey" | Changes submit key |
| No submission | Omit name | Control excluded |
| Element | Supported? | Notes |
|---|---|---|
<input> | Yes | All submittable types |
<textarea>, <select> | Yes | Multiline and dropdown |
<button> | Yes | Submit button name/value optional |
<form> | Yes | Legacy; prefer id for scripting |
<div>, <label> | No | Not form controls |
name vs id vs for| Attribute | Purpose | Must be unique? |
|---|---|---|
name | Form submission key | Unique per logical field; radios share name |
id | Document identifier, label target, CSS/JS hook | Yes, within the document |
for on <label> | Links label to control id | Points to an id, not name |
Login form field names, radio group with shared name, and changing element.name with JavaScript.
Type in fields and see how name becomes the submission key:
Would submit: email=user@example.com&message=Hello
Separate keys for username and password:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<button type="submit">Submit</button>
</form>The id connects labels and scripts; the name is what the server reads. Both attributes often appear on the same element.
Share one name across related options:
<fieldset>
<legend>Size</legend>
<label><input type="radio" name="size" value="s"> S</label>
<label><input type="radio" name="size" value="m"> M</label>
<label><input type="radio" name="size" value="l"> L</label>
</fieldset>The shared name groups radios so the browser treats them as one field. The old reference said names must always be unique—that is wrong for radio groups.
Change the submission key at runtime:
<input type="text" id="dynamicField" name="comment">
<script>
document.getElementById("dynamicField").name = "feedback";
</script>Renaming affects the next submission. Server-side handlers must expect the new key if you change name dynamically.
id with <label for> — Labels reference id, not name.<fieldset> and <legend> — Explain the shared name group to all users.email improve maintainability (users do not see name directly).Key on control.
Values entered.
name=value pairs.
Data processed by name.
The name attribute on form controls is fully supported in all browsers since the earliest HTML forms. Behavior for radio groups, multi-select, and file inputs is consistent across modern engines.
Every browser submits named form controls correctly.
Bottom line: Core HTML feature; essential for every form.
namename for radio groupsname with a unique id for labelsname with idname on <a> for anchors (obsolete)The name attribute is how form data gets labeled for the server. Every input, textarea, and select you want submitted needs a clear, purposeful name. Radio groups deliberately reuse the same name; other fields usually use distinct keys.
Keep id for labels and scripts, name for submission, and avoid obsolete anchor naming. With consistent names, your forms are easier to build, test, and process on the backend.
nameBookmark these before building forms.
name=value pairs.
PurposeServer vs DOM.
CompareSame name.
PatternRename field.
DynamicNot submitted.
Ruleusername=alex in the request data.id identifies the element in the page for labels and scripts. name identifies the field key sent to the server on submit.name groups them as one choice. Only the selected radio’s value is submitted.element.name = "fieldName" or setAttribute("name", "fieldName").id on the target element and href="#id". The a name pattern is obsolete.Practice login fields, radio groups, and dynamic element.name in the Try It editor.
5 people found this page helpful