Skip to content

Instantly share code, notes, and snippets.

@janbiasi
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janbiasi/ed264215dbfbc4030bdc to your computer and use it in GitHub Desktop.
Save janbiasi/ed264215dbfbc4030bdc to your computer and use it in GitHub Desktop.
Micro jQuery method for easy DOM selection without plugins on 11 lines
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