Created
April 18, 2013 20:31
-
-
Save PatrickJS/5415974 to your computer and use it in GitHub Desktop.
getElementsByClassName
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var getElementsByClassName = function(className) { | |
var element = arguments[1] || document.body, | |
results = [], | |
has_class_name = [].indexOf.call(element.classList, className) !== -1, | |
children_results = [].map.call(element.children, function(element) { | |
return getElementsByClassName(className,element)[0]; | |
}).filter(Boolean); | |
if (has_class_name) { | |
results.push(element); | |
} else { | |
results = results.concat(children_results); | |
} | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment