I'm just curious :)
One of the first things I learned in web development was jquery but right now I never use it. Even if I create a vanilla html/css/js website.
I'm just curious :)
One of the first things I learned in web development was jquery but right now I never use it. Even if I create a vanilla html/css/js website.
It requires craft to get addEventListener() on par with on().
jQuery comes with many implicits loops that save you time.
fadeIn() is easier to handle than the equivalent css. jQuery supports edge if you target it (things like prepend(), parent() work on it).
Error handling is just better. E.G: jQuery().val() will not raise an error if there is no match while document.getElementById().value will.
And probably a hundred of other little details that won't come out of my head but that will grind my gear if I start working on code.
Chaining is awesome to manipulate DOM. The imperative API is very verbose.
So. Many. Plugins.
I'm more of a VueJS guy now, but if I have only a quick page to setup, the 30ko of jquery are better than anything I can produce to wrap my repetitive code.
The fetch API is more powerful than the Jquery one.
Since forEach is implemented in es6 I just use that.
I see the point that you need it sometimes but still I would prefer some other lighter solutions.
ES6+ does not replace browser APIs. There's no ES6+ way around this:
const el = document.createElement(...)
el.setAttribute(...)
el.classList.toggle(...)
const parent = document.getElementById(...)
parent.appendChild(el)
You either do that in one line of jQuery, or end up writing your own wrappers if you need to do this more than once.It's more explicit—that will almost always win with me.
Once you write it not once, but twice, or five times, you will either switch to a lib/framework, or to jQuery, or will write your own wrapper not that different from jQuery.
insertElement(type: string, text?: string. attributes?: object, parent?: string): void;
I prefer writing something like that with some minor case-handling over pulling in a library every time I meet a repeatable fragment.If I know I'm going to run into a large host of needs, then it's a different story. But most of the time I find jQuery overkill and somewhat opaque.
I happily write them myself. It's a few seconds of work, really.
[1] Okay, there are some people, they're rare and generally regarded as weird.
Explicit [within the context of the language]. And by that I did mean human readable.
const divElement = document.createElement("div");
divElement.textContent = "Hello world";
document.appendChild(divElement);
Is more human readable to me than the jQuery abstraction (that pulls in piles of other potentially unused tooling) than: $(document).append("div").text("Hello, world");
It's more verbose, sure. But like I said in another comment, if I have to repeat the methods more than twice I'd probably just wrap the couple of lines into a function, like: function createTextElement(type: string, text: string): void {
const element = document.createElement(type);
element.textContent = text;
document.appendChild(element);
}
And anywhere that's called it's quite clear what is being done createTextElement("div", "Hello world");
This seems to be preferred when writing C as well, no? Rather than abstracting common methods to more opaque symbols?Maybe it's just me, but I prefer the English, descriptive version and I prefer working with code formatted the same way. The language (JS) has plenty of quirks as it is.
Comparing C/Assembly I don't think is a 1:1 fair comparison, though. Unless you're including TypeScript—which is how I tend to write JS anyway (whenever possible).
I wrote in a sibling comment:
Once you write it not once, but twice, or five times, you will either switch to a lib/framework, or to jQuery, or will write your own wrapper not that different from jQuery.
* Perhaps those 10 lines execute faster
* Perhaps those 10 lines are exactly 10 lines, where jQuery is 2 lines plus a 65k library
* Perhaps those 10 lines work equally in multiple environments (node, deno, electron, browser) where jQuery does not.
* Perhaps those 10 lines sit behind a custom abstraction that actually looks like a single method
* Perhaps those 10 lines do something jQuery does not
* Perhaps those 10 lines scale and extend in ways jQuery does not
* Perhaps those 10 lines have a desired side effect
Heh, and here we come full circle. I remember back when the whole point of jQuery was to handle the different underlying JS implementations for you.