English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM getElementsByClassName() Method

HTML DOM Element Object

element.getElementsByClassName()Returns an array-like object containing all child elements with the specified class name. When called on the document object, it searches the entire DOM document, including the root node. You can also call the getElementsByClassName() method on any element, which will return all child elements with the specified class name, rooted at the current element.

The elements in the returned list are sorted in the order they appear in the source code.

You can access the returned list by index number. The index starts from 0.

Usagelengthproperty determines the number of child elements with the specified class name, and then you can iterate over all elements and extract the required information.

Thedocument.getElementsByClassName()The working principle of the method is basically the same as the way it acts on the entire document, starting from the document root.

Syntax:

element.getElementsByClassName(class)
var gBox = document.getElementById("greenBox");
gBox.getElementsByClassName("demo")[0].innerHTML = "Hello World";
Test and see‹/›

Browser Compatibility

All browsers fully support the getElementsByClassName() method:

Methods
getElementsByClassName()YesYesYesYesYes

Parameter Value

ParameterDescription
classThe class name of the child element you want to obtain.
To search for multiple class names, separate them with spaces, for example, 'demo color'.

Technical Details

Return Value:Returns an array-like object of all child elements with all given class names.
DOM Version:DOM Level1

Related References

CSS TutorialCSS Syntax

CSS ReferenceCSS .classSelector

HTML DOM ReferenceclassName property

HTML DOM ReferenceclassList property

HTML DOM Referencedocument.getElementsByClassName()Methods

HTML DOM Referenceelement.getElementsByTagName()Methods

HTML DOM Element Object