jQuery 4.0 99% Complete(github.com) |
jQuery 4.0 99% Complete(github.com) |
> jQuery is used by 94.5% of all the websites whose JavaScript library we know. This is 77.4% of all websites.
Bootstrap does not require jQuery. Not sure if ever but not since 3.0 and were are at 5.0 at this point.
With that said, I'm quite surprised that jQuery is not abandoned, barely heard about it in the past decade or so. I suspect also that it is mostly part of boilerplate coming from other libraries and frameworks, and rarely intentional.
People keep telling me to switch to ES6 but I'm never upgrading. Why would I? jQuery just feels like a good pair of jeans.
$('#some-form').find('input').addClass('error')
Obviously you can do this in vanilla JS, but it's more cumbersome so you'll probably end up rewriting your own less complete version of jquery. There is some value to using a well worn library with public documentation that other developers can find/extend instead of a home grown solution.With that said, I wouldn't use it for a green field project. For sites that require lots of interactivity I would pick a modern front end framework that doesn't require this kind of manual dom traversal (probably phoenix/liveview).
document.getElementById('some-form').getElementsByTagName('input').forEach(i => i.classList.add('error'))
It is a little more verbose but it isn't that much more cumbersome today.That's also doing things "the right way" and not do-all selectors, but that option exists now, too:
document.querySelectorAll('#some-form > input').forEach(i => i.classList.add('error'))
If you want to make it a little less verbose: const $ = document.querySelectorAll
const addClass = (className) => (item) => item.classList.add(className)
$('#some-form > input').forEach(addClass('error')) document.querySelectorAll("#some-form input").forEach(e => e.classList.add("error"));Same thing of course applies to people who throw around the optional chaining operator without thinking to please TS.
Or would apply if one used a single selector here and document.querySelectorAll
The needless chaining here is a typical example of misleading jQuery code though. Looks as if it was there to prevent this issue.
for (const input of document.querySelectorAll('#some-form input') {
input.classList.add('error');
}
?In fact, this has an even earlier baseline browser compatibility than
NodeList.prototype.forEachI’m not a JS developer and I just have a basic understanding of what jQuery is used for, so I’d be interested in seeing some kind of historical timeline, both to better understand why jQuery was originally created, and to understand what purpose it might fill today (legacy dependencies excluded).
Admittedly I'm usually using it in places where loading the extra library is not an issue, so if you're really going for slimline, then sure.
However, it does matter in interactive websites because if the js is light it'll load fast and execute fast and you'll have interactivity sooner. It may be the case that images can be loaded later or arent even visible on the home page. And fonts of course will be replaced once they are loaded and the defaults will be used while it loads so it's progressive in a way.
Then again, how many folks are using jquery these days? It might be more rare than I think.
Browsers no longer do cross-site resource caching. https://www.stefanjudis.com/notes/say-goodbye-to-resource-ca...
Whether this whole bazaar is ideal or not is a separate issue :)
Of course now we have additional defensive programming tools like `?.` and `??` operators.
Point is that the DOM API version of the initial jQuery snippet are not equivalent. $() statement does a bit more than that.
This becomes obvious if the requirement are a bit more complex than the example, perhaps we want to chain more expression on at the end, then the DOM API version becomes more convoluted.