The Element method getElementsByClassName() returns a live HTMLCollection which contains every descendant element which has the specified class name or names.

The method getElementsByClassName() on the Document interface works essentially the same way, except it acts on the entire document, starting at the document root.


var elements = element.getElementsByClassName(names);


A DOMString containing one or more class names to match on, separated by whitespace.

Return value

An HTMLCollection providing a live-updating list of every element which is a member of every class in names.

Usage notes

As always, the returned collection is live, meaning that it always reflects the current state of the DOM tree rooted at the element on which the function was called. As new elements that match names are added to the subtree, they immediately appear in the collection. Similarly, if an existing element that doesn't match names has its set of classes adjusted so that it matches, it immediately appears in the collection.

The opposite is also true; as elements no longer match the set of names, they are immediately removed from the collection.

In quirks mode, the class names are compared in a case-insensitive fashion. Otherwise, they're case sensitive.


Matching a single class

To look for elements that include among their classes a single specified class, we just provide that class name when calling getElementsByClassName():


This example finds all elements that have a class of test, which are also a descendant of the element that has the id of main:


Matching multiple classes

To find elements whose class lists include both the red and test classes:

element.getElementsByClassName('red test');

Examining the results

You can use either the item() method on the returned HTMLCollection or standard array syntax to examine individual elements in the collection. However the following code will not work as one might expect because "matches" will change as soon as any "colorbox" class is removed.

var matches = element.getElementsByClassName('colorbox');

for (var i=0; i<matches.length; i++) {

Instead, use another method, such as:

var matches = element.getElementsByClassName('colorbox');

while (matches.length > 0) {

This code finds descendant elements with the "colorbox" class, adds the class "hueframe", by calling item(0), then removes "colorbox" (using array notation). Another element (if any are left) will then become item(0).

Filtering the results using array methods

We can also use methods of Array.prototype on any HTMLCollection by passing the HTMLCollection as the method's this value. Here we'll find all <div> elements that have a class of test:

var testElements = document.getElementsByClassName('test');
var testDivs = Array.prototype.filter.call(testElements, function(testElement) {
  return testElement.nodeName === 'DIV';


Specification Status Comment
The definition of 'Element.getElementsByClassName()' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
getElementsByClassNameChrome Full support 1Edge Full support 16
Full support 16
No support 12 — 16
Notes This function is implemented in the HTMLElement API, meaning non-HTML elements (like SVG elements) cannot use this function.
Firefox Full support 3
Full support 3
Notes Prior to Firefox 19, this method was returning a NodeList; it was then changed to reflect the change in the spec.
IE Full support 9
Full support 9
Notes This function is implemented in the HTMLElement API, meaning non-HTML elements (like SVG elements) cannot use this function.
Opera Full support 9.5Safari Full support 6WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 6Samsung Internet Android Full support 1.0


Full support  
Full support
See implementation notes.
See implementation notes.