Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Out of random interest, not even jQuery? I find it immensely tedious to write out vanilla js compared to all the shortcuts that $ provides...


Knowing that jQuery ends up calling those very same functions, only often after jumping through a bunch of hoops, doesnt make me consider it a shortcut. Between auto-complete and copy and paste, I prefer slightly more "work" up front, and then zero needless work at runtime, than a bit of "magic" that ends up doing needless work every single time it is run.

But I have to admit, I don't care about compatibility beyond Firefox on the desktop, if I wrote stuff for a company on a deadline, or if I worked with other people on something, I would mind jQuery very little or not at all. But for my own stuff I make to spark joy, where I get to decide all of it, I want it as vanilla as I can get it.


> copy and paste

Bravo for admitting it. I have to say I've usually run into trouble working with copy and paste coders though.

How do you make sure what you're doing this with is something you understand? I never understood this one.


I just meant copying and pasting long function and variable names out of my own code elsewhere, should autocomplete somehow not apply.


This line of reasoning only made sense in the old days.

For example, $.ajax() is strictly worse than the native window.fetch() -> Promise we have today.

I could see using jQuery in 2020 if your poor soul still has to support ancient browsers, but beyond that I suspect people only latch on to jQuery out of habit.


> I could see using jQuery in 2020 if your poor soul still has to support ancient browsers

IE 11 at work for some customer projects. It's dreadful. No support for anything decent, but at least the debugger is better than the "nothing" you had with IE6...


I'm not totally anti-jquery, but it helps to stay lightweight. If convenience is what you're after, you can always just bind querySelector and querySelectorAll to the document and assign each to a variable like this: https://dev.to/mrahmadawais/use-instead-of-document-querysel...


QuerySelectorAll is widely supported and you can make your own shorthand for it.

  function $(query) {
    const result = document.querySelectorAll(query)
    if ( result ) {
      if ( result.length > 1 ) {
        return result
      } else {
        return result[0]
      }
    }
  }


I hope you don't use that since it randomly returns either an element or a list of elements, so you'd have to also add an if/else every time you use it.

This is my go-to:

    const $ = document.querySelector.bind(document);
    const $$ = document.querySelectorAll.bind(document);
Tip: browser consoles already have these shortcuts, except $$ returns an actual array there instead of a NodeList.


The main advantage of jQuery these days is all the plugins it provides, most (if not all) things you need on a website exist in jQuery. And I also found it really has messed up search results, search for anything javascript related, and almost all answers you find on StackOverflow are people suggesting to use jQuery. (Even when the author didn't ask for it)

The other day I was looking for a simple markdown editor, and it took me a while to find a decent vanilla one. (Ended up using https://simplemde.com)


If they do this regularly, they surely have a personal toolbox of js code they can draw from that handle most everything jQuery would provide.


Yep, especially for handling events (which I try to use sparingly :)). That was one thing jQuery had a great out of the box API for.


Not even jQuery. The dom selector API is cohesive enough these days for my use.




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

Search: