getElementsByClassName() in IE8

Internet Explorer 8 (IE8) web browser does not support the getElementsByClassName() DOM method, so when trying to call the method, IE8 shows “Object doesn't support this property or method” error message in console.

However IE8 supports the querySelectorAll() method that allows to achieve the same goal without using any JS libraries:

var elems = document.getElementsByClassName('foo bar'); // IE9+
var elems = document.querySelectorAll('.foo.bar');      // IE8

In the example above, both calls select elements that have both foo and bar classes.

The getElementsByClassName() method is intended for selecting elements by one or multiple classes, while querySelectorAll() searches elements that match a selector.

In browsers that support both methods, using getElementsByClassName() to select elements by classes may be preferrable due to its potentially higher performance thanks to that there is no need for full-scale parsing of selector that may contain by far not only classes.


You may also be interested in matchesSelector() for IE8.