document.getElementsByClassName() is an instance method that returns every element with the given class name(s) (see MDN Document: getElementsByClassName()). Learn the live HTMLCollection, multiple class matching, element-scoped searches, how it compares to querySelectorAll, and five try-it labs.
01
Kind
Instance method
02
Arg
class name(s)
03
Returns
HTMLCollection
04
Live?
Yes
05
Multi-class
all required
06
Status
Baseline
Fundamentals
Introduction
When several elements share a class — cards, buttons, list items — getElementsByClassName collects them all in one array-like list.
MDN: the method returns an array-like object of all child elements that have all of the given class name(s). On document, the whole document is searched (including the root). You can also call it on any element to search only that subtree.
💡
Think: live group photo of matching classes
1) Pass "test" or "red test" 2) Get a live HTMLCollection 3) Read [0], length, or loop with for...of 4) Remember: DOM changes update the collection automatically
⚠️
Live collection warning (MDN)
This is a liveHTMLCollection. If an element stops matching (for example you remove its class), it disappears from the list. Be careful when iterating and mutating at the same time.
The result is not a single element — it is a collection. Use [0] when you want the first match (MDN).
Example 2 — Multiple classes (all required)
MDN: only elements that have every listed class are selected.
JavaScript
const matches = document.getElementsByClassName("orange juice");
for (const el of matches) {
console.log(el.textContent);
}
// Similar idea: document.querySelectorAll(".orange.juice")
Document.getElementsByClassName() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.getElementsByClassName()
Live HTMLCollection of elements matching one or more class names across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
getElementsByClassName()Wide
Bottom line: Use getElementsByClassName for simple class lookups. Prefer querySelectorAll for complex selectors or a static NodeList.
Wrap Up
Conclusion
document.getElementsByClassName(names) gathers every matching element into a live HTMLCollection. Pass one class or several (all required), optionally scope to a parent element, and remember the list updates as the DOM changes.
Remember the result is a collection, not a single element (MDN)
Use multiple class names when you need an AND match
Scope searches with an element root when possible
Copy to an array before mutating while iterating
Reach for querySelectorAll for richer selectors
❌ Don’t
Treat the return value as one element without [0]
Forget the collection is live (MDN warning)
Expect "a b" to mean a OR b — it means both
Assume Array methods exist without Array.from / borrowing
Use class lookups when a unique id is the right tool
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getElementsByClassName()
Live class matches as an HTMLCollection.
5
Core concepts
📝01
Returns
HTMLCollection
MDN
🔄02
Live
auto-updates
warning
🎯03
Multi
all classes
AND
⚡04
Scope
doc or element
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.getElementsByClassName() returns an array-like object of all child elements that have all of the given class name(s). On document, it searches the complete document.
No. MDN marks Document.getElementsByClassName() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A live HTMLCollection of found elements (MDN). Changes in the DOM are reflected in the collection as they occur.
MDN: pass a whitespace-separated string. Only elements that have ALL of those classes are selected (similar idea to .orange.juice in querySelectorAll).
Yes. MDN: you may call getElementsByClassName() on any element; it returns only matching descendants of that root.
Prefer querySelectorAll when you need more complex CSS selectors, a static NodeList snapshot, or modern Array methods without Array.prototype.call tricks.
Did you know?
MDN’s multiple-class demo pairs getElementsByClassName("orange juice") with querySelectorAll(".orange.juice") — two spellings of the same “must have both classes” idea.