JavaScript Document getElementsByName() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

document.getElementsByName() is an instance method that returns every element with a given name attribute (see MDN Document: getElementsByName()). Learn the live NodeList, form and radio-group patterns, how it differs from id/class lookups, and five try-it labs.

01

Kind

Instance method

02

Arg

name string

03

Returns

NodeList

04

Live?

Yes

05

Scope

Document

06

Status

Baseline

Introduction

HTML forms often reuse the same name — radio buttons, checkboxes, or hidden fields that travel together on submit. When you need every element that shares that name, call document.getElementsByName().

MDN: the method returns a NodeList collection of elements with a given name attribute in the document. The list is live: it updates as matching elements are added or removed.

💡
Think: group by form name

1) Markup uses name="up" (or any shared name)
2) Call document.getElementsByName("up")
3) Read [0], length, or loop the live list
4) Remember: name is not the same as unique id

📄
(X)HTML documents only (MDN)

MDN notes that the name attribute can only be applied in (X)HTML documents. The returned collection can include <meta>, <object>, and even elements that do not formally support name.

Related tutorials: getElementsByClassName(), getElementById(), getElementsByTagName().

Understanding document.getElementsByName()

An instance method on the Document object (MDN). Unlike class lookup, it is typically called on document only.

  • name — the value of the name attribute to match (MDN).
  • Return value — a live NodeList collection (MDN).
  • Live updates — new or removed matching elements refresh the list (MDN).
  • Shared names OK — radios and fields often share one name on purpose.
  • Not unique — for one unique id, use getElementById().
  • HTML contextname applies in (X)HTML documents (MDN).

📝 Syntax

General form of Document.getElementsByName (MDN):

JavaScript
getElementsByName(name)

Parameters

  • name — the value of the name attribute of the element(s) we are looking for (MDN).

Return value

A live NodeList collection — it automatically updates as new elements with the same name are added to, or removed from, the document (MDN).

MDN quick sample

JavaScript
const upNames = document.getElementsByName("up");
console.log(upNames[0].tagName); // "INPUT"

⚡ Quick Reference

GoalCode
Find by namedocument.getElementsByName("up")
First matchdocument.getElementsByName("up")[0]
Countdocument.getElementsByName("color").length
Loopfor (const el of document.getElementsByName("color")) { ... }
CSS alternativedocument.querySelectorAll('[name="up"]')
MDN statusBaseline Widely available (since Jan 2018)

🔍 At a Glance

Four facts about document.getElementsByName().

Returns
NodeList

live

Arg
name

string

Best for
forms

shared names

Status
Baseline

since 2018

📋 Live NodeList vs static query

getElementsByNamequerySelectorAll('[name=...]')
Result typeLive NodeList (MDN)Static NodeList
After DOM changeList updates automaticallyStays as captured at query time
Selector powerName onlyCan combine with other CSS filters
Beginner tipGreat for form groupsGreat for one-shot snapshots

Examples Gallery

Examples follow MDN Document: getElementsByName() and practical form patterns.

📚 Getting Started

Look up elements by their name attribute.

Example 1 — MDN: find name="up"

Classic MDN sample with hidden inputs.

JavaScript
const upNames = document.getElementsByName("up");
console.log(upNames.length);
console.log(upNames[0].tagName); // "INPUT"
Try It Yourself

How It Works

The method returns a collection, not a single node. Use [0] for the first match, just like other array-like DOM lists (MDN).

Example 2 — Radio group by shared name

Radios that share name form one choice group — perfect for this API.

JavaScript
const colors = document.getElementsByName("color");
console.log(colors.length); // 3

for (const radio of colors) {
  if (radio.checked) {
    console.log("selected:", radio.value);
  }
}
Try It Yourself

How It Works

One name string finds every radio in the group. Loop and check .checked to learn the user’s choice.

📈 Practical Patterns

Read values, watch live updates, and contrast with id.

Example 3 — Collect field values

Map each matching control to its current value.

JavaScript
const fields = document.getElementsByName("email");
const values = Array.from(fields).map((el) => el.value);
console.log(values);
Try It Yourself

How It Works

Convert the live NodeList with Array.from when you want familiar Array helpers like map and filter.

Example 4 — Live NodeList updates

Add another element with the same name and watch length grow.

JavaScript
const list = document.getElementsByName("item");
console.log("before:", list.length);

const extra = document.createElement("input");
extra.name = "item";
document.body.appendChild(extra);

console.log("after:", list.length); // live list grew (MDN)
Try It Yourself

How It Works

MDN: the collection automatically updates as new elements with the same name are added to, or removed from, the document.

Example 5 — name vs id

Same visual control can expose both attributes for different APIs.

JavaScript
// <input id="user-email" name="email">
const byId = document.getElementById("user-email");
const byName = document.getElementsByName("email");

console.log(byId === byName[0]); // often true for a single field
console.log(byName.length);      // could be > 1 if name is reused
Try It Yourself

How It Works

Use id when you need one unique node. Use name when the browser (and your form) should group controls together.

🚀 Common Use Cases

  • Radio groups — find every option sharing one name.
  • Form helpers — read or clear related fields in one pass.
  • Hidden inputs — MDN-style lookups for named hidden controls.
  • Validation — check that at least one radio in a group is selected.
  • Prefer querySelectorAll — for static snapshots or richer CSS filters.
  • Not unique ids — use getElementById() for one id.

🧠 How getElementsByName() Works

1

Pass a name string

The value must match the element’s name attribute (MDN).

Input
2

Search the document

Collects matching nodes across the (X)HTML document (MDN).

Scope
3

Build a live NodeList

Additions and removals keep the collection in sync (MDN).

Live
4

Use index, length, or loops

Read [0], count matches, or convert with Array.from.

📝 Notes

  • MDN: Baseline Widely available since January 2018.
  • MDN: returns a live NodeList.
  • MDN: name can only be applied in (X)HTML documents.
  • MDN: the list may include meta, object, and elements that do not formally support name.
  • Shared names are normal for form controls; unique ids are for getElementById.
  • Related: getElementsByClassName(), getElementById(), getElementsByTagName().

Browser Support

Document.getElementsByName() is Baseline Widely available on MDN (since January 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Document.getElementsByName()

Live NodeList of elements matching a name attribute across all major browsers.

Baseline Widely available
Google Chrome Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Microsoft Edge Supported
Yes
Opera Supported
Yes
Internet Explorer Supported (legacy)
Yes
getElementsByName() Wide

Bottom line: Use getElementsByName for form name groups. Prefer getElementById for unique ids, or querySelectorAll for static CSS-based lookups.

Conclusion

document.getElementsByName(name) gathers every element that shares a name attribute into a live NodeList. It shines for form groups and radios — and pairs well with id/class lookups when you need other selection styles.

Continue with getElementsByClassName(), getElementsByTagName(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use shared name values for radio/checkbox groups
  • Treat the result as a collection ([0], length)
  • Remember the list is live (MDN)
  • Convert with Array.from when you need Array helpers
  • Pick getElementById when you truly need one unique node

❌ Don’t

  • Confuse name with unique id
  • Assume a single element without checking length
  • Forget the collection updates when the DOM changes (MDN)
  • Expect XML-only documents to behave like HTML name forms
  • Skip null/empty checks when no matches exist

Key Takeaways

Knowledge Unlocked

Five things to remember about getElementsByName()

Live name-attribute matches as a NodeList.

5
Core concepts
🔄02

Live

auto-updates

MDN
🎯03

Arg

name value

attribute
04

Best for

form groups

radios
🛡05

Status

Baseline

2018

❓ Frequently Asked Questions

MDN: Document.getElementsByName() returns a NodeList collection of elements with a given name attribute in the document.
No. MDN marks Document.getElementsByName() as Baseline Widely available (since January 2018). It is not Deprecated, Experimental, or Non-standard.
A live NodeList collection (MDN). It automatically updates as elements with that name are added to or removed from the document.
No. id should be unique and is found with getElementById. name can be shared (for example radio buttons) and is found with getElementsByName.
MDN: the name attribute can only be applied in (X)HTML documents. The collection can still include elements that do not formally support name.
Prefer querySelectorAll('[name="..."]') when you want a static NodeList, CSS selector power, or to combine name with other filters.
Did you know?

MDN’s tiny demo uses two hidden inputs — name="up" and name="down" — then logs document.getElementsByName("up")[0].tagName, which prints INPUT.

Next: getElementsByTagName()

Learn how to collect every element of a given tag name into a live HTMLCollection.

getElementsByTagName() →

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.

6 people found this page helpful