Not all browsers support getElementsByClassName , although the situation is improving. You can use a function that checks its own implementation and uses it if it is found, or it captures all the elements and checks each of them for the class name, returning an array of those that match.
function getElementsByClassName( className, context ) { //the context is the container we will confine our search to (optional) context = context || document; //use native implimentation if it exists if( context.getElementsByClassName ) { return context.getElementsByClassName( className ); //returns a nodeList } //we have to do it ourselves if we get here var candidates = context.getElementsByTagName( '*' ); var found = []; //regular expression to match the classname as per comments var rxp = new RegExp( "(?:^|\\s)" + className + "(?:\\s|$)"); for( var i = 0, l = candidates.length; i < l; i++ ) { if( rxp.test( className ) { found.push( candidates[i] ); } } return found; //returns an array of nodes }
meouw source share