Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getElementsByClassName() doesn't work in old Internet Explorers like IE6, IE7, IE8

The following code:

var borderTds = document.getElementsByClassName('leftborder');

gives me an error message in Internet Explorer 6, 7 and 8:

Object does not support this method

How can I select elements by their class in these browsers?

I prefer not to use JQuery.

like image 604
code511788465541441 Avatar asked Jul 05 '11 14:07

code511788465541441


People also ask

Why does document getElementsByClassName not work?

There are several issues: Class names (and IDs) are not allowed to start with a digit. You have to pass a class to getElementsByClassName() . You have to iterate of the result set.

What is getElementsByClassName () used for?

The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.

What is the difference between getElementsByClassName and querySelectorAll?

querySelectorAll successfully removes the classes from all the elements, but getElementsByClassName only removes the classes from about half the elements.

How do you get the first element of getElementsByClassName?

If you want only the first element in the DOM with that class, you can select the first element out of the array returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0];


2 Answers

IE6, Netscape 6+, Firefox, and Opera 7+ copy the following script in your page:

document.getElementsByClassName = function(cl) {
  var retnode = [];
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) {
    if((' ' + elem[i].className + ' ').indexOf(' ' + cl + ' ') > -1) retnode.push(elem[i]);
  }
  return retnode;
}; 
like image 140
Peter Avatar answered Oct 06 '22 01:10

Peter


This solution may help. This is a custom getElementsByClassName function implemented in pure javascript, that works in IE.

Essentially what this script is doing is probing, one by one, all possible options and picks the best one available. These options are:

  1. Native document.getElementsByClassName function.
  2. document.evaluate function, which allows evaluation of XPath queries.
  3. Traversing the DOM tree.

Of course the first one is the best performance-wise, however the latter should be available everywhere including IE 6.

Usage example, which is also available on the page, looks like this:

getElementsByClassName("col", "div", document.getElementById("container")); 

So the function allows 3 parameters: class (required), tag name (optional, searches for all tags if not specified), root element (optional, document if not specified).

Update. The solution linked in the blog post is hosted on the Google Code which is shutting down in Jan 2016. However the author has made it available on GitHub. Kudos to flodin pointing this out in the comments.

like image 37
Andrei Avatar answered Oct 06 '22 00:10

Andrei