Hacker News new | past | comments | ask | show | jobs | submit login

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.



I know it doesn't replace jQuery, but this is fairly straightforward:

  $ = document.querySelector.bind(document)
  $$ = document.querySelectorAll.bind(document)


It'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.


You 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.


Yeah, but 19 helper functions later and you've got half of jQuery. I'd rather use a properly tested library.


Using this since ages. Also use window.DOM = document


One could also save document.querySelector to $ as a constant.


That would be very confusing for unsuspecting reader. Please use something different, I would suggest `qs` for `querySelector` and `qsa` for `querySelectorAll`.


This 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.


> 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: