document.getElementsByName(name) returns a liveNodeList of every element whose name attribute matches the string you pass (MDN). It is ideal for form controls that share a name, such as radio groups.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.getElementsByName. Start with this line.
JavaScript
document.getElementsByName(name)
Call
What it does
document.getElementsByName(name)
Primary signature.
document.getElementsByName(...)
Call with the arguments your case needs.
Built into the browser. Call getElementsByName(...) on document — no import required.
Library declarations
JavaScript
document.getElementsByName(name)
Parameters
Parameter
Description
name
Required. Value of the name attribute to match (MDN)
Return Value
Return Value
Description
*
Live NodeList
JavaScript Examples
Example 1 — Find Elements by name
MDN-style lookup: collect inputs with name="up" and read the first tag.
Match the attribute. The string "up" must equal the element’s name (MDN).
Get a collection. The method returns a NodeList, not a single node.
Read the first item.[0] is the first match; .tagName prints INPUT.
Check length. Use .length before assuming a match exists.
Example 2 — Read a Radio Group
Radios that share one name form a choice group — perfect for this API.
JavaScript
const colors = document.getElementsByName("color");
console.log(colors.length); // 3
let selected = "(none)";
for (const radio of colors) {
if (radio.checked) selected = radio.value;
}
console.log(selected); // e.g. "blue"
Store the list once. You keep a reference to the live NodeList.
Add a match. A new input with name="item" enters the document.
Length updates. MDN: the collection updates as matching elements are added or removed.
Static alternative.querySelectorAll('[name="item"]') would stay frozen at query time.
Compatibility
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.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementsByName()Wide
Bottom line: Safe for form name groups today. Prefer getElementById for unique ids, or querySelectorAll for static CSS-based lookups. See the MDN Document.getElementsByName() reference.
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 returned collection can include meta, object, and even 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.