Create getElementsByClassName() function for use in IE

The getElementsByClassName() is already present in some modern browsers, but this code from Kevin Yank at Sitepoint just adds it for those browsers that don’t already have it…. function getElementsByClassName(className)...

The getElementsByClassName() is already present in some modern browsers, but this code from Kevin Yank at Sitepoint just adds it for those browsers that don’t already have it….

function getElementsByClassName(className) {
     // get all elements in the document
     if (document.all) {
         var allElements = document.all;
     }
     else {
         var allElements = document.getElementsByTagName("*");
         }

     var foundElements = [];

     for (var i = 0, ii = allElements.length; i < ii; i++) {
         if (allElements[i].className == className) {
             foundElements[foundElements.length] = allElements[i];
         }
     }

     return foundElements;
}

Related posts:

  1. JavaScript Quick Syntax Check
  2. Create a JavaScript Array
  3. JavaScript Dot Operator List
  4. Speeding Up ‘For Loops’
  5. Adding jQuery to your page

This post has had 80 views