News for hackers. Stuff that matters.
Re:jQuery 3.6.0 by bcrosby95 on Tuesday March 02, 2021 @02:28PM (#26319342)
> This has been rolled into the Javascript document api via document.querySelector, and document.querySelectorAll.

Have you actually compared the two methods in a real website? It's like saying why use Ruby when you can use Java. The Javascript document API is super verbose. No thanks.

Re:jQuery 3.6.0 by Kaze404 on Tuesday March 02, 2021 @02:36PM (#26319462)
Conversely, looking at how you do it through jQuery after seeing how it is in Javascript gives me the same reaction.

The fact that jQuery is usually assigned to the dollar sign definitely doesn't help, especially for beginners. The amount of questions I've answered that stemmed from a fundamental misunderstanding of how Javascript works, all because they learned jQuery instead of Javascript, is pretty significant.

Re:jQuery 3.6.0 by mettamage on Tuesday March 02, 2021 @02:40PM (#26319523)
I remember when I was in uni that the $() must've had a special meaning or something. I was pretty convinced that the $ was some tough mathematical operator or computer science concept that I didn't yet understand.

When I learned that it was simply a function invocation that returned a jQuery object so you could chain it, I was absolutely shocked about the simplicity of that statement.

Beginning developers sure like to project their cognitive insecurities onto programming. Or at least, I did :)

Re:jQuery 3.6.0 by macintux on Tuesday March 02, 2021 @02:50PM (#26319648)
There's so much magic in programming that as a beginner, it's hard to know which magic you should dig deeper into, vs which is such a treacherous rabbit hole you're better off cargo culting.
Re:jQuery 3.6.0 by bigfudge on Tuesday March 02, 2021 @04:40PM (#26321240)
It think this is true of many technical things. Statistics, sailing, car mechanics, photography... What I love about HN is it often gives clues as to which are the interesting rabbit holes.
Re:jQuery 3.6.0 by cosmotic on Tuesday March 02, 2021 @02:54PM (#26319726)
That argument stands true for most frameworks on most platforms. I prefer the dollar sign; it's pervasive and concise.
Re:jQuery 3.6.0 by Kaze404 on Tuesday March 02, 2021 @04:52PM (#26321420)
I disagree. There's nothing about React, Vue or any other framework that I'm aware of that does anything similar to jQuery's dollar sign. The only magically-looking abstraction I can think of is JSX, and that's not only not mandatory but very obviously something that's off the curve. Unlike the dollar sign, which just looks like a special construct of the language.
Re:jQuery 3.6.0 by Xevi on Tuesday March 02, 2021 @08:37PM (#26323641)
Re:jQuery 3.6.0 by Kaze404 on Tuesday March 02, 2021 @09:09PM (#26323847)
Wow, that looks weird. Thank you, I hadn't heard of Svelte before.
Re:jQuery 3.6.0 by cosmotic on Tuesday March 02, 2021 @11:10PM (#26324649)
No one blames you for not knowing about the 18275th javascript framework.
Re:jQuery 3.6.0 by cphoover on Tuesday March 02, 2021 @02:35PM (#26319454)
You don't have to use all of it though, right? You can choose to only only use the applicable parts: eg:

``` $('.container-element') .addClass('active');

// vs

document.querySelectorAll('.my-class-selector') .classList.add('active') ```

I use the native APIs all the time, they are not that complicated or overly verbose. Easily memorized with practice IMHO. Also they are all extremely well documented on MDN.

Re:jQuery 3.6.0 by lhorie on Tuesday March 02, 2021 @02:44PM (#26319566)
Technically that breaks, you want:

    document.querySelectorAll('.my-class-selector').forEach(el => el.classList.add('active'))
Personally I use the native APIs because they aren't that hard to use (most of them, anyways) and I tend to be OCD about bundle sizes, but to each their own, I suppose.
Re:jQuery 3.6.0 by megous on Tuesday March 02, 2021 @03:25PM (#26320137)
Yeah, oh so many years ago I realized that I use just a small subset of jQuery, and just rewrote those bits, to avoid having to inject whole of jQuery into all pages that I browse (I had some global https?://* userscripts, so I was basically injecting jQuery into every single page and frame).

https://megous.com/dl/tmp/basic.js

1.3kB compressed. Much better. :) And I avoided having to rewrite my extensions with platform APIs. That was before FF killed all the joy in writing userscripts for me, because I was no longer able to install and update them just by copying them to mozilla folder and had to invoke a lot of clicking ceremony to just get my 20 scripts or so from git into firefox past the webextension limitations on storage/access to mozilla folder.

Re:jQuery 3.6.0 by akie on Wednesday March 03, 2021 @03:12AM (#26325924)
You should totally open-source that, put it on NPM, and write an article or so about it.
Re:jQuery 3.6.0 by megous on Friday March 05, 2021 @03:43PM (#26361831)
Everyone will have different set of methods they use. Point of this is that if you need some method you can just add it in in 4-5 lines or remove one you don't need. So it's not the best fit for NPM. It's good fit for good old copy/paste into a project, if you're optimizing for size/memory use. :)
Re:jQuery 3.6.0 by maple3142 on Tuesday March 02, 2021 @09:02PM (#26323804)
BTW, NodeList.prototype.forEach is not available on every browsers that supports querySelector/querySelectorAll, such as IE11: https://caniuse.com/mdn-api_nodelist_foreach

So you need to be careful about writing code like that if you want to avoid jQuery.

Re:jQuery 3.6.0 by andrewmd5 on Tuesday March 02, 2021 @09:46PM (#26324092)
We just don’t bother supporting IE11.
Re:jQuery 3.6.0 by malinens on Wednesday March 03, 2021 @01:24PM (#26332417)
you are probably in smaller company. still at least 1 percent of users use it. 1% of revenue increase in big companies is substantial amount of money
Re:jQuery 3.6.0 by vbezhenar on Tuesday March 02, 2021 @02:50PM (#26319663)
Your example does not work. querySelectorAll returns array and you can't write `array.classList.add("...")`, because array does not have that property. You need to iterate over that array. And that's exactly why jQuery is so much more convenient over native API: you can just treat collections of elements like an element and jQuery will iterate for you if necessary.

Another jQuery advantage is pseudo-selectors which don't exist in CSS.

There are plenty of convenient one-liners in jQuery which will expand to plenty of lines in native API. That said, with modern API and JS features it's not that bad either.

Re:jQuery 3.6.0 by t_tsonev on Tuesday March 02, 2021 @03:07PM (#26319892)
Just a nitpick, but querySelectorAll returns a NodeList.

This can bite at times, for example IE supports Array.forEach, but not NodeList.forEach

Re:jQuery 3.6.0 by dceddia on Tuesday March 02, 2021 @03:19PM (#26320055)
Yeah this gets me every time. You can .forEach a NodeList, but can't .map. I usually end up wrapping it in an array and calling map on it like [...theNodeList].map(...)
Re:jQuery 3.6.0 by mkl on Tuesday March 02, 2021 @03:06PM (#26319883)
querySelectorAll returns a NodeList, which is even more limited than an array (e.g. no map or filter).
Re:jQuery 3.6.0 by ROARosen on Tuesday March 02, 2021 @03:24PM (#26320134)
but [...querySelectorAll()] does return an array and is just as concise
Re:jQuery 3.6.0 by lhorie on Tuesday March 02, 2021 @04:05PM (#26320724)
but beware of ASI :)
Re:jQuery 3.6.0 by cortesoft on Tuesday March 02, 2021 @03:00PM (#26319792)
I think the fact that your very simple example (which is already longer and more complicated in your version) doesn't work shows the advantage of jQuery
Re:jQuery 3.6.0 by dan-robertson on Tuesday March 02, 2021 @02:44PM (#26319574)
Isn’t your example a type error. JQuery’s operations auto-broadcast over the lists of elements it returns whereas the native methods don’t
Re:jQuery 3.6.0 by frabert on Tuesday March 02, 2021 @02:34PM (#26319426)
How so? Who stops you from doing something like `const $ = document.querySelector`?
Re:jQuery 3.6.0 by Epskampie on Tuesday March 02, 2021 @02:39PM (#26319512)
There's a ton of other functions you need like element.addEventListener, insertAdjacentElement, XMLHttpRequest (when you need progress) etc. etc. At that point you might as well save yourself and the person after you a major headache and just use jQuery whose API everybody knows.
Re:jQuery 3.6.0 by cortesoft on Tuesday March 02, 2021 @03:01PM (#26319814)
The big magic of jQuery is that you can act on an array of elements as if it were a single element. Your alias does not give you that at all.
Re:jQuery 3.6.0 by cphoover on Tuesday March 02, 2021 @02:39PM (#26319501)
Good point
Re:jQuery 3.6.0 by runj__ on Tuesday March 02, 2021 @02:50PM (#26319655)
I'm still a little bit sad we got fetch as part of javascript when the request module had such a great interface. I still prefer the look of it.

https://www.npmjs.com/package/request

Re:jQuery 3.6.0 by bilekas on Tuesday March 02, 2021 @02:48PM (#26319631)
> The Javascript document API is super verbose.

Can't say I've ever heard that as an issue before..

Also I think the main point was there are so many better frameworks that manage the DOM better than jQuery as the need has evolved. I'll always have fond memories of jQuery but I don't see it as my go-to anymore, by a long shot.