Skip to content

Instantly share code, notes, and snippets.

@nickL
Last active December 17, 2015 08:19
Show Gist options
  • Save nickL/5579175 to your computer and use it in GitHub Desktop.
Save nickL/5579175 to your computer and use it in GitHub Desktop.
domSelect.js -- (super-light jquery like dom selection)
window.$ = function(a) {
var b, c;
b = {
"#": "getElementById",
".": "getElementsByClassName",
"@": "getElementsByName",
"=": "getElementsByTagName",
"*": "querySelectorAll"
};
c = /[=#@.*]/.exec(a)[0];
return document[b[c]](a.split(c)[1]);
};
@nickL
Copy link
Author

nickL commented May 14, 2013

Example:

  • $('#iddiv')* // get element by id.
  • $('.classdiv') // get by class name.
  • $('@namediv') // get by element name.
  • $('=div') // get by element tag name.
  • $('?div div.inside') // get element by query selector.
  • $('#iddiv').getAttribute('name') // getAttribute of name.
  • $('.classdiv')[0].getAttribute('name') // getAttribute of name from nodelist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment