-
-
Save tblobaum/1455538 to your computer and use it in GitHub Desktop.
tiny select. Selecting has never been so awesome \o/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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