Skip to content

Instantly share code, notes, and snippets.

@tblobaum
Forked from Raynos/select.js
Created December 10, 2011 16:38
Show Gist options
  • Save tblobaum/1455538 to your computer and use it in GitHub Desktop.
Save tblobaum/1455538 to your computer and use it in GitHub Desktop.
tiny select. Selecting has never been so awesome \o/
// Pretty fast - http://jsperf.com/select-vs-natives-vs-jquery/2
/*
Selector function.
@param String selector - selector to use. Works with "#foo", ".bar" and "tag".
@param Node context - optional context to restrict search to, defaults to document.
@return Node|NodeList
*/
function select (selector, context) {
var c = selector.charAt(0),
method,
context = context || document;
if (c === '#') {
selector = selector.substring(1);
method = context.getElementById(selector);
}
else if (c === '.') {
selector = selector.substring(1);
method = context.getElementsByClassName(selector);
}
else {
method = context.getElementsByTagName(selector);
}
return method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment