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;
}

No comments: