JavaScript Element getElementsByClassName() Method
Beginner
5 min read
Updated: Nov 2026
3 examples
Method
Definition
In JavaScript, the getElementsByClassName() method returns a liveHTMLCollection of every descendant that has the given class name or names (MDN). Calling it on an element scopes the search to that element’s subtree.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.getElementsByClassName. Start with this line.
JavaScript
el.getElementsByClassName(names)
Call
What it does
el.getElementsByClassName(names)
Primary signature.
el.getElementsByClassName(...)
Call with the arguments your case needs.
Built into the browser. Call getElementsByClassName(...) on an Element — no import required.
Library declarations
JavaScript
el.getElementsByClassName(names)
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
Live HTMLCollection
JavaScript Examples
Example 1 — Scoped Search
MDN pattern: search for test only under #main.
JavaScript
const main = document.getElementById("main");
const matches = main.getElementsByClassName("test");
console.log(matches.length);
console.log(matches[0].textContent);
Capture the collection once. You don’t need to call the method again.
Mutate the DOM. Append a new element with the same class.
Length updates automatically. That is what “live” means (MDN).
Contrast with querySelectorAll. A NodeList snapshot would stay at the old length.
Compatibility
Browser Support
Element.getElementsByClassName() is Baseline Widely available (MDN: across browsers since October 2017). Safe to use in modern evergreen browsers.
✓ Baseline · Widely available
getElementsByClassName()
Live HTMLCollection of descendant elements matching one or more class names.
99%Widely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
getElementsByClassName()Excellent
Bottom line: Use for fast class lookups in a subtree; use querySelectorAll when you need CSS selectors or a static snapshot.
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 of the element you call it on (MDN).
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.