In modern browsers you can get them by using the getElementsByClassName function:
var elements = document.getElementsByClassName('myClass');
for (var i = 0, n = elements.length; i < n; i++) {
//..
}
Notice that I'm getting the elements.length
only once, that is a common practice when iterating HTMLCollections.
That's because those collections are "live", they can change at any time, and accessing the length property on each iteration it's expensive.
For complete cross-browser implementations, check this article by Mr. Resig:
Edit: I leave you here a refactored version of the Dustin Diaz getElementsByClassName cross-browser pure DOM Implementation:
function getElementsByClassName(node,classname) {
if (node.getElementsByClassName) { // use native implementation if available
return node.getElementsByClassName(classname);
} else {
return (function getElementsByClass(searchClass,node) {
if ( node == null )
node = document;
var classElements = [],
els = node.getElementsByTagName("*"),
elsLen = els.length,
pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
})(classname, node);
}
}