5 ms·
I never understood why people say “We’ve solved your jQuery problem” by introducing getElementsByClassName() or even querySelector(). When I come back to my old
by laurent92 6y ago
I never understood why people say “We’ve solved your jQuery problem” by introducing getElementsByClassName() or even querySelector(). When I come back to my older app, $(“.my-class”) is still much nicer. jQuery gets out of the way, the class gets syntax-highlighted, and .focus() can’t create an NPE.
- wishinghand 6y agoOne could also save document.querySelector to $ as a constant.
- vbezhenar 6y agoThat would be very confusing for unsuspecting reader. Please use something different, I would suggest `qs` for `querySelector` and `qsa` for `querySelectorAll`.
- wishinghand 6y agoThis method is not so much for multi-dev codebases but for single devs working on their personal or low-tech sites and are having a hard time letting the $('some-selector') syntax go.
- tyingq 6y agoI 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.
- josteink 6y ago> jQuery gets out of the way, the class gets syntax-highlighted, and .focus() can’t create an NPE. So if your selector is wrong, you won’t actually know, because your code will pretend to run along just fine. I personally consider that a con, not a pro, and one of my (many) reasons not to use jQuery.