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

I really don't get this discourse, jQuery API is much more ergonomic, concise and clearer than the DOM api. Sure you can do it but that doesn't mean you should


I think it's a bit of both. jQuery served the purpose of making web development more sane back in the day by handling all browser quirks. Part of that was the nice syntax.

I personally have tried to drop jQuery, but truthfully, its syntax is just much easier to use. Nowadays, I use Cash https://github.com/fabiospampinato/cash to give me the nice syntax without the bloat. It strikes the perfect balance for me.


> I personally have tried to drop jQuery, but truthfully, its syntax is just much easier to use.

It's not just the syntax, it's also the expression-heavy and chained API which makes it much more flexible. As well as the set-oriented approach, which can lead to performance issues if you're not careful but is generally extremely enjoyable.

The DOM is an extremely procedural, plodding API, you need to name everything because you always need to set attributes and call non-chainable side-effecting APIs, not to mention the fecking NodeList which has to be converted to an array to do basically anything useful (in part because Javascript never really embraced iterators, and instead uses arrays for everything still).

I guess you could mitigate that with an "API adapter", but...


Although writing jQuery is still shorter (And likely will forever be). Editor (like vscode) nowadays is much more smarter then the era.

A lot of API with long names is no longer a issue to type. You just mark the variable as a element. After that, autocomplete handles the rest for you. Shorten the gap with experience of using jQuery. If developer tools is still as dumb as that era. I would think the transition is impossible.

And there are also more frameworks to enhance experience of style modification or templating. Back to then, there are only some dumb template that do initial rendering. But framework these days also handles update for you. So you don't really need to modify it directly by yourself unless in some edge case.


I did a javascript project targeting web/blackberry and early ios. We used jQuery to smooth out the kinks (or that's what our lead told us). It worked as expected.

Went from disliking it to really liking the syntax. Its pretty clear and easy to use, which is why I still use it. We don't use it extensively becaue we're still on the "submit form, get page back with javascript graph and data tables table, development model. We probably should transition to a framework, but they evolve so quickly ...


Just replace that one liner with a 15 liner.


document.querySelector('html')

And for React: https://javascript.info/custom-elements


Other functions are much more difficult to replace

    $('.sth').closest('ul')
To me jQuery is like Regex or Linq. I think it had a really good api.



Is not the same thing as it is a Element method, not NodeList one, so you will would need to map it instead. Because NodeList doesn't have the .map method you would also need to transform it to a Array first.

So the actually drop-in replacement of `$(foo).closest(bar)` would be something like:

    Array.from(document.querySelector(foo)).map(el => el.closest(bar))


This did not exist the last time I looked. TIL. Thank you!


Is it harder or just a bit longer-winded? E.g. i'm thinking this is the same behaviour

    document.getElementsByClassName('sth')[0]?.closest('ul');


That's not exactly the same, I don't think, because jQuery will give you all the `ul` that are closest to `.sth`, not just the first. You'd have to write a loop over the list returned from `document.getElementsByClassName('sth')` to get the same behaviour (because it doesn't look like the standardised `closest` works on lists.)


If i'm following right, i think this expression would match?

    Array.from(document.getElementsByClassName("sth")).map(e => e.closest("ul"))
The jQuery is definitely nicer to read than this though


Yeah, that seems to do the same.


Thanks, this is now standard it seems. TIL


And you would replace $.getJSON by how many lines?


are we doing CORS? Adding headers? If it is just a one-off fetch, I'd probably long-hand it. If I intended to be fetching from many endpoints, I'd probably write a tiny little abstraction. I'm not exactly keen on loading a 30kb lib (gzipped) so that I can write (maybe) fewer lines for data fetching. Especially considering jQuery's async requests don't conform to the fetch standard. (They reject on 404s)


It's not because you know how to do a hello world that you know how to program.


Yes, the DOM API is painful to work with.



Hah. My first "web apps" were CGI scripts back in 1995. I know enough about the DOM to know that it sucks: document.getElementById is too verbose. I can either define my own function as a shortcut, or use a standard approach for the same. jQuery is that standard approach.


I think that the improved vanilla js is now "satisficing" for more people. Their threshold of ergonomic usability has been achieved and they stop looking:

https://en.wikipedia.org/wiki/Satisficing


or the curse of being good enough. Good concept! :)


Ad hoc manipulation of the DOM contradicts the philosophy of a lot of frameworks, as that is state which is unaccounted for.




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

Search: