DEV Community

Discussion on: Use $ & $$ Instead of document.querySelector/All in JavaScript without jQuery

Collapse
 
tgwizman profile image
Tgwizman

I have been using
$ = function(e) {return document.getElementById(e)}
for years. Now I started using
$ = function(e) {
switch (e[0]) {
case '.': return document.getElementsByClassName(e.substring(1, e.length)); break;
case '#': return document.getElementById(e.substring(1, e.length)); break;
default: return document.getElementsByTagName(e);
}
}