2 ms·
I know it doesn't replace jQuery, but this is fairly straightforward: $ = document.querySelector.bind(document) $$ = document.querySelectorAll.bind(documen
by tyingq 6y ago
I know it doesn't replace jQuery, but this is fairly straightforward:
$ = document.querySelector.bind(document)
$$ = document.querySelectorAll.bind(document)
- vijaybritto 6y agoUsing this since ages. Also use window.DOM = document
- arp242 6y agoIt's annoying that it returns a NodeList which doesn't have some common array-y methods and the like. While these aliases are certainly convenient, the jQuery semantics are much nicer and more intuitive IMO.
- tyingq 6y agoYou can, of course, make it more robust, like: function $$(sel, parent) { var nodes = (parent || document).querySelectorAll(sel); return Array.prototype.slice.call(nodes, 0); } Similarly, you can get rid of $$ and fold it all into $, etc. I was trying to be concise in the original response.
- arp242 6y agoYeah, but 19 helper functions later and you've got half of jQuery. I'd rather use a properly tested library.