Tuesday, August 28, 2007

getElementsByClassName My Eesy Edition

Take it easy

function getElementsByClassName(className, tag){
var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
var tag = tag || "*";

var elements = (tag == "*" && document.all)? document.all : document.getElementsByTagName(tag);
var returnElements = [];
var current;
var length = elements.length;
for(var i=0; i
current = elements[i];
if(testClass.test(current.className)){
returnElements.push(current);
}
}
return returnElements;
}

getElementsByClassName Delux Edition

GET it FROM http://muffinresearch.co.uk/archives/2006/04/29/getelementsbyclassname-deluxe-edition/

function getElementsByClassName(strClass, strTag, objContElm) {
strTag = strTag || "*";
objContElm = objContElm || document;
var objColl = objContElm.getElementsByTagName(strTag);
if (!objColl.length && strTag == "*" && objContElm.all) objColl = objContElm.all;
var arr = new Array();
var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
var arrClass = strClass.split(delim);
for (var i = 0, j = objColl.length; i < j; i++) {
var arrObjClass = objColl[i].className.split(' ');
if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
var c = 0;
comparisonLoop:
for (var k = 0, l = arrObjClass.length; k < l; k++) {
for (var m = 0, n = arrClass.length; m < n; m++) {
if (arrClass[m] == arrObjClass[k]) c++;
if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
arr.push(objColl[i]);
break comparisonLoop;
}
}
}
}
return arr;
}