Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript document.getElementsByClassName compatibility with IE

What is the best method to retrieve an array of elements that have a certain class?

I would use document.getElementsByClassName but IE does not support it.

So I tried Jonathan Snook's solution:

function getElementsByClassName(node, classname) {     var a = [];     var re = new RegExp('(^| )'+classname+'( |$)');     var els = node.getElementsByTagName("*");     for(var i=0,j=els.length; i<j; i++)         if(re.test(els[i].className))a.push(els[i]);     return a; } var tabs = document.getElementsByClassName(document.body,'tab'); 

...but IE still says:

Object doesn't support this property or method

Any ideas, better methods, bug fixes?

I would prefer not to use any solutions involving jQuery or other "bulky javascript".

Update:

I got it to work!

As @joe mentioned the function is not a method of document.

So the working code would look like this:

function getElementsByClassName(node, classname) {     var a = [];     var re = new RegExp('(^| )'+classname+'( |$)');     var els = node.getElementsByTagName("*");     for(var i=0,j=els.length; i<j; i++)         if(re.test(els[i].className))a.push(els[i]);     return a; } var tabs = getElementsByClassName(document.body,'tab'); 


...Also if you only need IE8+ support then this will work:

if(!document.getElementsByClassName) {     document.getElementsByClassName = function(className) {         return this.querySelectorAll("." + className);     };     Element.prototype.getElementsByClassName = document.getElementsByClassName; } 

Use it just like normal:

var tabs = document.getElementsByClassName('tab'); 
like image 708
Web_Designer Avatar asked Sep 14 '11 03:09

Web_Designer


People also ask

What does getElementsByClassName () do in Javascript?

The getElementsByClassName() method returns an array-like of objects of the child elements with a specified class name. The getElementsByClassName() method is available on the document element or any other elements. The method returns the elements which is a live HTMLCollection of the matches elements.

What data type does document getElementsByClassName return?

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 getElementById and getElementsByClassName?

getElementById returns a single DOM element whose ID matches your query. getElementsByClassName returns an HtmlCollection - an array-like structure containing the DOM elements that matched your query. You have to iterate through each element in the array to apply your style.

Does getElementsByClassName return NodeList?

The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object.


2 Answers

It's not a method of document:

function getElementsByClassName(node, classname) {     var a = [];     var re = new RegExp('(^| )'+classname+'( |$)');     var els = node.getElementsByTagName("*");     for(var i=0,j=els.length; i<j; i++)         if(re.test(els[i].className))a.push(els[i]);     return a; }  tabs = getElementsByClassName(document.body,'tab');  // no document 
like image 131
Joe Avatar answered Sep 28 '22 18:09

Joe


you may create the function for older browsers

if (typeof document.getElementsByClassName!='function') {     document.getElementsByClassName = function() {         var elms = document.getElementsByTagName('*');         var ei = new Array();         for (i=0;i<elms.length;i++) {             if (elms[i].getAttribute('class')) {                 ecl = elms[i].getAttribute('class').split(' ');                 for (j=0;j<ecl.length;j++) {                     if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {                         ei.push(elms[i]);                     }                 }             } else if (elms[i].className) {                 ecl = elms[i].className.split(' ');                 for (j=0;j<ecl.length;j++) {                     if (ecl[j].toLowerCase() == arguments[0].toLowerCase()) {                         ei.push(elms[i]);                     }                 }             }         }         return ei;     } } 
like image 22
gdarcan Avatar answered Sep 28 '22 18:09

gdarcan