# \[JS\] document.getElementsByClassName()

**URL:** <https://forum.kirupa.com/t/js-document-getelementsbyclassname/80544>\
**Category:** open source\
**Created:** [February 22, 2005, 11:11pm UTC](https://forum.kirupa.com/t/js-document-getelementsbyclassname/80544 "2005-02-22T23:11:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![iloveitaly](https://avatars.discourse-cdn.com/v4/letter/i/7ea924/32.png) [@iloveitaly](https://forum.kirupa.com/u/iloveitaly)\
**Post date:** [February 22, 2005, 11:11pm UTC](https://forum.kirupa.com/t/js-document-getelementsbyclassname/80544/1 "2005-02-22T23:11:44Z")

</div>

heres a equivelent of document.getElementsByTagName(), except it gets all the elements with the specified class name.  
[AS]  
var IE = false;  
if(navigator.appName.indexOf(“IE”)!=-1 && !document.hasAttribute) IE = true;

/\*\*  
\*@description: gets all elements by the specified class name  
\*@param: name, a string specifieing the class name you want to grab  
\*@returns: an array of found elements if successfull, false on failure  
\*\*/  
document.getElementsByClassName = function(name) {  
var findStack = [];

```
function checkChildren(ob) {
	for(var a = 0; a&lt;ob.childNodes.length; a++) {
		if(ob.childNodes[a].nodeType!=1) 
			continue;
		if(((IE)?ob.childNodes[a].attributes['class'].nodeValue:ob.childNodes[a].getAttribute("class"))==name)
			findStack.push(ob.childNodes[a]);
		if(ob.childNodes[a].hasChildNodes()) 
			checkChildren(ob.childNodes[a]);
	}
}

checkChildren(this.getElementsByTagName("body")[0]);

if(findStack.length==0) return false;

return findStack;

```

}  
[/AS]
