Newer browsers (IE9 and above) have support for document.getElementsByClassName
(which ironically means that it has less support than querySelectorAll
, but I digress...), but that probably won't meet your compatibility needs. In this case, you can use something like this to get an Array of nodes with the class.
var nodes = document.getElementsByTagName('*'),
targetClass = 'myclass',
result = [];
for(var i = 0, l = nodes.length; i < l; i++){
if((nodes[i].className + ' ').indexOf(targetClass + ' ') !== -1){
result.push(nodes[i]);
}
}
This is not an exact duplicate of getElementsByClassName
, since that function returns a NodeList
, but it should be close enough for most purposes.