HTML name Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

Introduction

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.

What You’ll Learn

01

Form key

Submit label.

02

name vs id

Server vs DOM.

03

Radio groups

Same name.

04

Checkboxes

Shared or unique.

05

No name

Not submitted.

06

.name JS

Rename field.

Purpose of name Attribute

The 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.

💡
Not for page anchors

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.

📝 Syntax

Add name with a string identifier on form controls:

name.html
<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>

Syntax Rules

  • Value is a string, e.g. name="email" or name="user[first]" for nested keys in some frameworks.
  • Required on controls you want included in form submission (except submit button without name, which is optional).
  • Radio group: same name, different value on each radio.
  • id is for document identity and <label for>; name is for submitted data.
  • JavaScript IDL property: inputElement.name = "newKey".
  • Duplicate names on text inputs are allowed but can confuse server parsing—avoid unless intentional.

💎 Values

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).
name-js.html
document.getElementById("dynamicField").name = "feedback";

document.querySelector("input").setAttribute("name", "comment");

⚡ Quick Reference

Use caseMarkupNotes
Text field<input name="username">Submitted as username=…
Radio groupname="size" value="m"Same name on each radio
Hidden field<input type="hidden" name="token">Still needs name
Label pairingid + forNot name (use id)
JS renameel.name = "newKey"Changes submit key
No submissionOmit nameControl excluded

Applicable Elements

ElementSupported?Notes
<input>YesAll submittable types
<textarea>, <select>YesMultiline and dropdown
<button>YesSubmit button name/value optional
<form>YesLegacy; prefer id for scripting
<div>, <label>NoNot form controls

name vs id vs for

AttributePurposeMust be unique?
nameForm submission keyUnique per logical field; radios share name
idDocument identifier, label target, CSS/JS hookYes, within the document
for on <label>Links label to control idPoints to an id, not name

Examples Gallery

Login form field names, radio group with shared name, and changing element.name with JavaScript.

👀 Live Preview

Type in fields and see how name becomes the submission key:

Would submit: email=user@example.com&message=Hello

Example — Named Form Controls

Separate keys for username and password:

form-name.html
<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>
Try It Yourself

How It Works

The id connects labels and scripts; the name is what the server reads. Both attributes often appear on the same element.

Example — Radio Button Group

Share one name across related options:

radio-name.html
<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>
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Change the submission key at runtime:

dynamic-name.html
<input type="text" id="dynamicField" name="comment">



<script>

  document.getElementById("dynamicField").name = "feedback";

</script>
Try It Yourself

How It Works

Renaming affects the next submission. Server-side handlers must expect the new key if you change name dynamically.

♿ Accessibility

  • Use id with <label for> — Labels reference id, not name.
  • Group radios with <fieldset> and <legend> — Explain the shared name group to all users.
  • Descriptive names help developers — Clear keys like email improve maintainability (users do not see name directly).
  • Do not rely on name for visible labels — Always provide visible text or aria-label where needed.
  • Stable names — Avoid renaming fields unexpectedly; it can confuse autofill and assistive workflows.

🧠 How name Works

1

Author sets name

Key on control.

Markup
2

User fills form

Values entered.

Input
3

Form submits

name=value pairs.

Submit
=

Server reads keys

Data processed by name.

Browser Support

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.

HTML4+ · Fully supported

Universal form identification

Every browser submits named form controls correctly.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
name on form controls 100% supported

Bottom line: Core HTML feature; essential for every form.

💡 Best Practices

✅ Do

  • Give every submittable control a meaningful name
  • Use the same name for radio groups
  • Pair name with a unique id for labels
  • Use consistent naming conventions across your app
  • Validate submitted keys on the server

❌ Don’t

  • Confuse name with id
  • Use name on <a> for anchors (obsolete)
  • Expect unnamed controls to appear in submission data
  • Give every radio a different name (breaks grouping)
  • Rename fields dynamically without updating server logic

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about name

Bookmark these before building forms.

5
Core concepts
🔗 02

vs id

Server vs DOM.

Compare
🔘 03

Radio group

Same name.

Pattern
⚙️ 04

.name JS

Rename field.

Dynamic
🚫 05

No name

Not submitted.

Rule

❓ Frequently Asked Questions

It sets the key used when a form control’s value is submitted, e.g. username=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.
Same name groups them as one choice. Only the selected radio’s value is submitted.
element.name = "fieldName" or setAttribute("name", "fieldName").
The control is not included in form submission data (except some button edge cases).
No. Use id on the target element and href="#id". The a name pattern is obsolete.

Label form data with the name attribute

Practice login fields, radio groups, and dynamic element.name in the Try It editor.

Try form fields example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful