Skip to content

Instantly share code, notes, and snippets.

@danheberden
Forked from ralphholzmann/gist:1038554
Created June 21, 2011 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danheberden/1038635 to your computer and use it in GitHub Desktop.
Save danheberden/1038635 to your computer and use it in GitHub Desktop.
Challenge Accepted.
getEl = (function (doc) {
var getElementsByClassName = (function () {
return doc.getElementsByClassName ?
function (selector) {
return doc.getElementsByClassName(selector.split('.').pop());
} : function (selector) {
var parts = selector.split("."),
tags = document.getElementsByTagName(parts[0] || "*"),
matches = [],
classes, i, iLength, c, cLength;
for (i = 0, iLength = tags.length; i < iLength; i++) {
classes = ("" + tags[i].className).split(" ");
for (c = 0, cLength = classes.length; c < cLength; c++) {
if (classes[c] == parts[1]) {
matches.push(tags[i]);
break;
}
}
}
return matches;
}
})();
return doc.querySelectorAll ?
function (selector) {
return doc.querySelectorAll(selector);
} : function (selector) {
var id;
if ((id = selector.split("#")).length > 1) {
return document.getElementById(id.pop());
} else {
return getElementsByClassName(selector);
}
}
})(document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment