Skip to content

Instantly share code, notes, and snippets.

@bendc
Created August 26, 2014 21:57
Show Gist options
  • Save bendc/059f59303ab5d1e3eaab to your computer and use it in GitHub Desktop.
Save bendc/059f59303ab5d1e3eaab to your computer and use it in GitHub Desktop.
A fast method to select DOM elements
function fastSelect(selector) {
if (selector == "body") {
return document.body
}
else if (selector == "head") {
return document.head
}
else if (/^[\#.]?[\w-]+$/.test(selector)) {
switch (selector[0]) {
case "#":
return document.getElementById(selector.slice(1))
case ".":
return document.getElementsByClassName(selector.slice(1))
default:
return document.getElementsByTagName(selector)
}
}
return document.querySelectorAll(selector)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment