Skip to content

Instantly share code, notes, and snippets.

@PatrickJS
Created April 18, 2013 20:31
Show Gist options
  • Save PatrickJS/5415974 to your computer and use it in GitHub Desktop.
Save PatrickJS/5415974 to your computer and use it in GitHub Desktop.
getElementsByClassName
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