Skip to content

Instantly share code, notes, and snippets.

@darthwade
Last active August 29, 2015 14:04
Show Gist options
  • Save darthwade/82d89666dc4ece05be4f to your computer and use it in GitHub Desktop.
Save darthwade/82d89666dc4ece05be4f to your computer and use it in GitHub Desktop.
Polyfill for Element.getElementsByClassName(search)
if (!Element.prototype.getElementsByClassName) {
Element.prototype.getElementsByClassName = function (search) {
var d = this, elements, pattern, i, results = [];
if (d.querySelectorAll) { // IE8
return d.querySelectorAll("." + search);
}
if (d.evaluate) { // IE6, IE7
pattern = ".//*[contains(concat(' ', @class, ' '), ' " + search + " ')]";
elements = d.evaluate(pattern, d, null, 0, null);
while ((i = elements.iterateNext())) {
results.push(i);
}
} else {
elements = d.getElementsByTagName("*");
pattern = new RegExp("(^|\\s)" + search + "(\\s|$)");
for (i = 0; i < elements.length; i++) {
if (pattern.test(elements[i].className)) {
results.push(elements[i]);
}
}
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment