Skip to content

Instantly share code, notes, and snippets.

@rixth
Forked from nonowarn/by.js
Created February 3, 2011 18:11
Show Gist options
  • Save rixth/809892 to your computer and use it in GitHub Desktop.
Save rixth/809892 to your computer and use it in GitHub Desktop.
(function (win, doc) {
var by = {},
slice = [].slice,
type2method = {
id: "getElementById",
className: "getElementsByClassName",
tagName: "getElementsByTagName",
query: "querySelectorAll"
};
for (var type in type2method) {
if (type2method.hasOwnProperty(type) && type2method[type] in doc) {
(function (type, method) {
by[type] = method.match(/(?:Elements|All)/) ? function () {
return slice.call(doc[method].apply(doc, arguments));
} : function () {
return doc[method].apply(doc, arguments);
};
})(type, type2method[type]);
}
}
win.by = by;
})(window, document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment