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
Fundamentals
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.
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);
}
}
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
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.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementsByName()Wide
Bottom line: Use getElementsByName for form name groups. Prefer getElementById for unique ids, or querySelectorAll for static CSS-based lookups.
Wrap Up
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.