Return to Snippet

Revision: 26337
at April 23, 2010 09:29 by jasonseney


Initial Code
getElementsByClassName: function(classname, baseNode, tag) {
	baseNode = baseNode || document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = baseNode.getElementsByTagName(tag || "*");
	for (var i = 0, j = els.length; i < j; i++)
		if (re.test(els[i].className)) a.push(els[i]);
	return a;
}

Initial URL

                                

Initial Description
Similar to [Get Elements by Class Name]( http://snipplr.com/view/1696/get-elements-by-class-name/), but with optional "tag" parameter. Performance is increased by first retrieving the desired tag if available.

Initial Title
Javascript DOM - getElementsByClassName

Initial Tags
javascript, DOM

Initial Language
JavaScript