Last active
August 29, 2015 14:03
-
-
Save janbiasi/ed264215dbfbc4030bdc to your computer and use it in GitHub Desktop.
Micro jQuery method for easy DOM selection without plugins on 11 lines
This file contains hidden or 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
var $ = function(selector) { | |
var matches = { | |
'#': 'getElementById', // $('#myId') | |
'.': 'getElementsByClassName', // $('.myClass') | |
'@': 'getElementsByName', // $('@myName') | |
'=': 'getElementsByTagName', // $('=body') | |
'?': 'querySelectorAll' // $('?anything') | |
}, rex = /[=#@.*]/.exec(selector)[0]; | |
var nodes = document[matches[rex]](selector.split(rex)[1]); | |
return nodes.length > 1 ? nodes : nodes[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment