-
-
Save jed/991057 to your computer and use it in GitHub Desktop.
select DOM elements by ID, tag name, or class name
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
// inspired by @techpriester: https://gist.github.com/988627 | |
function( | |
a, // take a simple selector like "name", "#name", or ".name", and | |
b // an optional context, and | |
){ | |
a = a.match(/^(\W)?(.*)/); // split the selector into name and symbol. | |
return( // return an element or list, from within the scope of | |
b // the passed context | |
|| document // or document, | |
)[ | |
"getElement" + ( // obtained by the appropriate method calculated by | |
a[1] | |
? a[1] == "#" | |
? "ById" // the node by ID, | |
: "sByClassName" // the nodes by class name, or | |
: "sByTagName" // the nodes by tag name, | |
) | |
]( | |
a[2] // called with the name. | |
) | |
} |
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
function(a,b){a=a.match(/^(\W)?(.*)/);return(b||document)["getElement"+(a[1]?a[1]=="#"?"ById":"sByClassName":"sByTagName")](a[2])} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "cssSelect", | |
"description": "select DOM elements by ID, tag name, or class name", | |
"keywords": [ | |
"dom", | |
"css", | |
"id", | |
"class", | |
"tag" | |
] | |
} |
You can save another byte by removing ^
. Due to how the regular expressions is executed this does not make a difference in your case. And by the way, I tried to create getElementsByClassName
in 140 bytes, which is missing in Internet Explorer <9.0, but currently it still is 162 bytes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would advice you about that IE<9 doesn't support
getElementsByClassName
(source: caniuse.com) contrary to rudylattae comment.