Monday, June 25, 2007

getElementsByClass

Heres a little piece of JavaScript I found at :
http://javascript.about.com/library/bldom08.htm

I'm sure it can be of use to someone else out there. This coded creates your own getElementsByClass method which is not built into javascript.

document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i <>
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};

No comments: