For work that needs a little enhancement on top of server-side HTML, but not a full-blown JS UI framework, jQuery is a small price to pay for a stable, reliable, and cross-browser compatible dependency.
For work that needs a little enhancement on top of server-side HTML, but not a full-blown JS UI framework, jQuery is a small price to pay for a stable, reliable, and cross-browser compatible dependency.
Yes, the vanilla selectors are more verbose, but any half decent code editor will make them just as fast to type. I don't mean to be penantic, but I don't really understand why so many people still use jQuery at all.
And what are we saving by eschewing it and going vanilla? One little 30kb-gzipped javascript file - smaller than most jpegs. I agree for super simple tasks it might be best to skip it.
The DOM API is a sloppy and hastily-designed pile of crap from the OOP era that we're unfortunately stuck with for legacy reasons. jQuery shows what it could have been.
Ends up eliminating 50-60% of the code, and the time for writing and reading it.
As for jquery I haven’t needed it much since stimulusjs but loved it whenever I used it.
wat
DOM APIs have been, and still are, 90s-era OOP style with zero attempt at being functional, declarative, or composable.
Even the newly developed API are usually stuck in the same mindset (see everything developed for web components)
(Almost) none of the DOM APIs are functional. They are literally `object.methodCall()`
DOM APIs are the embodiment of 90s-era OOP. There's literally nothing functional about them. All [1] "functions" are methods defined on very specific objects. You can't even get a proper reference to them without binding them to specific object instances
What exactly is functional about this?
const newDiv = document.createElement("div");
const newContent = document.createTextNode("Hello, world");
newDiv.appendChild(newContent);
const currentDiv = document.getElementById("div1");
document.body.insertBefore(newDiv, currentDiv);
Okay, here's an easier question. What will happen here, and why? const function_reference = document.getElementById;
function_reference("#id");
[1] Technically speaking, not all all. The more recent `fetch` is probably the only piece of browser APIs that can be called functional for some very limited definition of functionalMost (all?) other "global" functions are defined on the instance of the window object: getComputedStyle, getSelection etc.
A series of function calls that each do something and each returns a value.
> Okay, here's an easier question. What will happen here, and why?
A function call that does something and returns a value, because in this language complex types are passed by reference.
Doesn't make it functional
> A function call that does something and returns a value,
Ah, so you don't know.
Here's what will actually happen:
TypeError: Can only call Document.getElementById on instances of Document
Because it's a method on an object. And you have to bind that method to a specific object instance before you can call it.If it was functional this would never be the case. But since this is 90s-era OOP, you have to do this:
const function_reference = document.getElementById.bind(document);
function_reference("#id");This is not what functional programming is. Functional programming is a completely different paradigm for writing code, and it _is_ declarative by definition. The Wikipedia definition is pretty decent:
Functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.
The "Comparison to imperative programming" section[1] offers a decent example of the paradigm, but DOM manipulation in JavaScript is pretty much as imperative as it gets. Having functions is necessary, but not sufficient, for functional programming.[1]: https://en.wikipedia.org/wiki/Functional_programming#Compari...
Functional programming uses stateless, immutable transformations to contorl data flow. The second you instantiate an object with some internal properties that change in memory, you are no longer programming functionally
You can modify the code to set the display property as inline-block, but now you need a different show function for every possible display value you want to restore. Or you need to build a layer of state to remember what the original value was and hey you've rewritten the jquery version.
And this is all ignoring that the jquery versions just look so much simpler to remember and are cleaner to read.
```css .hide { display: none important!; } ```
```js function hide(el) { el.classList.add('hide'); } ```
You'll likely be adding 6-700KB of React and its ilk anyway (I see it in every project now).
With jQuery it feels like a lot of functionality is hidden away, which can lead to unexpected situations that are harder to debug.
In my experience writing vanilla code takes less time, not more, because I have a better grip on what is happening.
The great things about these plugins is that you can strip them down to what you need and patch them yourself (remember lib or vendor directory). NPM and build tools make this a chore.
It could be argued that avoiding unnecessary work is my main job function. It's a lot more efficient to just get something that already does what I need, without requiring patches. In my experience leaving the jQuery ecosystem made this a lot easier.
Because it has a beautifully designed API. I don't directly use jQuery usually anymore. But I use the API in a basic DOM wrapper I've written. https://gist.github.com/pseudosavant/b86eedd9960ade958d49447...
I'm convinced this site is actually satire and it designed to promote jQuery. In nearly every example, the native version is far more verbose.
These days I find vanilla far easier to read and it is a lot more clear to me what is actually happening in the code.
Depending on jQuery limits your ability to work with code that does not use it. Ie a lot of the more professional libraries.
For me dropping jQuery feels like a step up. Why don't you just try it, see what happens. It's just a day of being a little bit annoyed and then you should be fine.
jQuery might be a crutch, sure, but I'm not sure crutches are a bad thing. There's no reason to make something harder than it has to be. I just like how some things take far fewer key stroke and are less verbose, but still readable.
Readability is subjective. There are people that think Perl is fine and I think it ranks as one of the least readable languages to ever reach popular status. Meanwhile, I think Python is the most readable language to have ever been created, and somehow some people struggle with it, and tbh it kind of blows my mind.
Users do notice the difference is large JS framework driven applications.
There's a large proportion of posters on HN who spend their days building web apps. They are very likely to stick to using what they know, even when building web sites.
If you spend your days working as a welder, and suddenly need to build a box for some reason, you're probably not going to be breaking out the fine wood joinery tools. You're probably going to take some sheet steel and weld up a box and move on to whatever else you have to do.
It's not a problem, it's not a fault, it just how things are.
Not the GP, but that's why, when applicable, I try to prefix my replies with a variant of "I agree, and btw..."
proceeds to name selection and native functions
no need for build tools and all headache. (i know you do that with react but its not comparable features).
for a small project those efficiencies wont make a difference on my $20 godaddy hosting account.
i dont want some tool or process getting in my way. if i have a problem then i will look for a solution.
I do like SPA also, but some of the nicest projects I've worked with could be run locally by launching `python3 -m http.server 8008' on the right folder.
I'm happier when I can do something without a processing step, or the need for a package.json. Need a third-party script? Vendor it, and never have to worry about supply-chain attacks.