plainJS

Functions / Selecting

Select elements by class name

jQuery $('.foo')

2015 page

This is the original page from 2015, kept at its address while the tested rewrite is in progress. The code is of its time: it ran in Internet Explorer and does not use what browsers have gained since. The rewrite replaces it here.

In short

getElementsByClassName – a fast way of selecting DOM elements by class name in modern browsers. Does not work in IE 8 and below.

The getElementsByClassName() method returns a collection of elements with the given class name as a NodeList. It is supported in modern browsers. Internet Explorer has support for this method as of version 9. Example:

js
var list = document.getElementsByClassName('foo');

// get the number of selected elements
console.log(list.length);

// iterate over elements and output their HTML content
for (var i=0; i<list.length; i++)
    console.log(list[i].innerHTML);

As with getElementById(), it’s also possible to select elements by class name within another container:

js
var container = document.getElementById('header');
var list = container.getElementsByClassName('foo');

Warning: Watch out for the pitfall when looping through elements selected by class name and – at the same time – modifying the class attribute.

The method is fast, but due to the limitation of selecting elements only by class name, its usefulness is limited. querySelectorAll() is a convenient method for accessing DOM elements by almost arbitrary CSS selectors.