Document

document.getElementsByClassName

Returns a live HTMLCollection of all child elements which have all of the given class names

Syntax

JavaScript
document.getElementsByClassName(classNames)

Parameters

ParameterTypeDescription
classNamesstringA string of space-separated class names to match

Return Value

A live HTMLCollection of found elements

Examples

Basic Usage
const items = document.getElementsByClassName('highlight')
console.log(items.length)
Practical Example
const cards = document.getElementsByClassName('card active')
Array.from(cards).forEach(card => {
  console.log(card.textContent)
})
Advanced Usage
const warnings = document.getElementsByClassName('warning')
while (warnings.length > 0) {
  warnings[0].classList.remove('warning')
}

Understanding document.getElementsByClassName

The document.getElementsByClassName method in JavaScript returns a live HTMLCollection of all child elements which have all of the given class names. It belongs to the Document object and is one of the most widely used methods for working with document values in modern JavaScript and TypeScript applications.

The method signature is document.getElementsByClassName(classNames). It accepts 1 parameter: classNames. When called, it returns a live htmlcollection of found elements. Understanding when and how to use getElementsByClassName() helps you write more expressive, readable code.

Common use cases for document.getElementsByClassName include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-queryselectorall, dom-getelementsbytagname, dom-getelementbyid, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for document.getElementsByClassName is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Document Methods

Other methods in the Document object

Related Tools

More Document Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.