JavaScript Element getElementsByClassName() Method
Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method
Overview
What You’ll Learn
Element.getElementsByClassName() is an instance method that finds descendant elements matching one or more class names and returns a live HTMLCollection. Learn scoped searches, multiple classes, live-collection pitfalls, comparison with querySelectorAll(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
HTMLCollection
03
Args
class names
04
Scope
Descendants
05
Live
Auto-updates
06
Status
Baseline widely
Fundamentals
Introduction
When you need every element with a certain CSS class inside a section of the page, getElementsByClassName() is a direct, readable option. It searches only descendants of the element you call it on—not the element itself.
The return value is a liveHTMLCollection. If matching elements are added or removed from the subtree, the collection updates automatically (MDN).
💡
Beginner tip
Modern code often uses querySelectorAll('.class'), which returns a staticNodeList. Prefer getElementsByClassName() when you want a live collection, or when you already have a parent element and a simple class lookup.
This page is part of JavaScript Element. Document.getElementsByClassName() works the same way from the document root (MDN).
Concept
Understanding the getElementsByClassName() Method
Calling el.getElementsByClassName(names) walks the subtree under el and collects every descendant whose class list matches names.
It is an instance method on Element.
names — one or more class names, whitespace-separated (MDN).
Returns a liveHTMLCollection.
Multiple classes mean the element must include all of them.
Case-sensitive in standards mode; case-insensitive in quirks mode (MDN).
Also available on Document for whole-page searches (MDN).
Foundation
📝 Syntax
General form of Element.getElementsByClassName (MDN):
JavaScript
getElementsByClassName(names)
Parameters
names — a string containing one or more class names to match, separated by whitespace.
Return value
A live HTMLCollection of every descendant element that is a member of every class in names (MDN).
Common patterns
JavaScript
// Single class
const items = container.getElementsByClassName("item");
// Multiple classes (must have both)
const cards = container.getElementsByClassName("card featured");
// Scoped under #main (MDN)
const tests = document.getElementById("main")
.getElementsByClassName("test");
// Access items
console.log(items.length);
console.log(items[0]);
Cheat Sheet
⚡ Quick Reference
Goal
Code
Find by one class
el.getElementsByClassName("item")
Multiple classes
el.getElementsByClassName("a b")
Scoped search
parent.getElementsByClassName("x")
Count matches
collection.length
Static alternative
el.querySelectorAll(".item")
MDN status
Baseline Widely available (since October 2017)
Snapshot
🔍 At a Glance
Four facts to remember about Element.getElementsByClassName().
The search runs only on descendants of container, not on container itself unless it also has the class.
Example 2 — Scoped Search Under #main
MDN: limit results to descendants of a specific element.
JavaScript
const main = document.getElementById("main");
const tests = main.getElementsByClassName("test");
console.log(tests.length);
// Only .test elements inside #main
MDN: a for loop with i++ skips elements because the live collection shrinks when you remove the matching class. Always process item(0) or copy to a static array first.
Element.getElementsByClassName() is Baseline Widely available (MDN: across browsers since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.getElementsByClassName()
Find descendant elements by class with a live HTMLCollection — scope searches to any element subtree.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
getElementsByClassName()Excellent
Bottom line: Use getElementsByClassName() for simple class lookups in a subtree. Remember the collection is live — copy to an array or use while(matches.length) when mutating during loops.
Wrap Up
Conclusion
Element.getElementsByClassName() returns a live HTMLCollection of descendant elements matching the class names you pass. It is a straightforward way to query by class within any element subtree.
Use while (collection.length) when removing matches in a loop
Copy to an array ([...collection]) for safe iteration
Pass multiple classes as one whitespace-separated string
Prefer querySelectorAll for complex CSS selectors
❌ Don’t
Assume the collection is static like querySelectorAll
Use a forward for loop while removing matching classes
Forget descendants-only — the caller element is not included
Pass CSS selectors with dots (".item") — use plain class names
Rely on case-insensitivity unless you are in quirks mode
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getElementsByClassName()
Live class-based descendant search.
5
Core concepts
📝01
Returns
HTMLCollection
API
📄02
Live
auto-update
DOM
✍️03
Scope
descendants
Subtree
✅04
Baseline
widely available
Status
⚡05
Modern
querySelAll
Static
❓ Frequently Asked Questions
It returns a live HTMLCollection of descendant elements whose class list includes the specified class name or names. The search is limited to the subtree rooted at the element you call it on.
No. MDN marks Element.getElementsByClassName() as Baseline Widely available (across browsers since October 2017). It is not Deprecated, Experimental, or Non-standard.
A string with one or more class names separated by whitespace. With multiple names, elements must include every listed class (MDN).
getElementsByClassName() returns a live HTMLCollection that updates when the DOM changes. querySelectorAll() returns a static NodeList snapshot.
Yes. MDN: matching elements are added or removed from the collection immediately when the DOM subtree changes.
Yes. Document.getElementsByClassName() works the same way but searches from the document root. Element.getElementsByClassName() searches only descendants of that element.
Did you know?
MDN notes that the returned HTMLCollection is live: when you add or remove matching elements from the subtree, the collection updates immediately. That is the main difference from querySelectorAll().