It has been a long time since you said "You Don't Need jQuery", but it's still useful for doing DOM operation in user script etc.1
Get elements
In native, element getter methods has too many kinds and has too long name.
In jQuery, $()
is all.
Get element by selector
Native
const viewport = document.querySelector('meta[name=viewport]');
jQuery
const $viewport = $('meta[name=viewport]');
Get elements by selector
Native
const externalLinks = document.querySelectorAll('a[target=_blank]');
jQuery
const $externalLinks = $('a[target=_blank]');
Get elements by class
Native
const articles = document.getElementsByClassName('article');
jQuery
const $articles = $('.article');
Get element by id
Native
const main = document.getElementById('main');
jQuery
const $main = $('#main');
Get elements by name
Native
const buttons = document.getElementsByName('button');
jQuery
const $buttons = $('[name=button]');
Get elements by tag name
Native
const links = document.getElementsByTagName('a');
jQuery
const $links = $('a');
Set styles
In native, You can't set multiple styles to element at once.
In jQuery, jQuery.css()
can that.
Native
element.style['background-color'] = '#000000';
element.style['color'] = '#ffffff';
jQuery
$element.css({
'background-color': '#000000',
'color': '#ffffff'
});
Create element from HTML string
In native, You need to cast long spell to create element from HTML string.
In jQuery, $()
can that.
Native
const foo = document.createRange().createContextualFragment(`<div id="foo">Foo</div>`);
jQuery
const $foo = $(`<div id="foo">Foo</div>`);
Set display state
In native, You need to rewrite value of display property to change display state of element.
In jQuery, there are several intuitive methods.
Show element
Native
element.style.display = 'block'; //or 'inline', 'inline-block', 'inline-table'...
jQuery
$element.show();
Hide element
Native
element.style.display = 'none';
jQuery
$element.hide();
Toggle display
Native
const elementIsShowing = element.ownerDocument.defaultView.getComputedStyle(element, null).display !== 'none';
element.style.display = elementIsShowing ? 'none' : 'block'; //or 'inline', 'inline-block', 'inline-table'...
jQuery
$element.toggle();
Other Language
-
In my own application, I will use Vue.js etc.Β β©
Latest comments (36)
Another reason why we need jQuery is that every (almost) method returns jQuery object allowing method chaining.
Like this:
$('div').find('p').parent().click(function(){..});
The above code is simple, concise, intuitively readable.
Simple code is the base for complex systems.
Now, go rewrite it in pure JavaScript and feel the difference.
Here is a list, jQuery to JS plainjs.com/javascript/
So. If you are just lazy you need to use jQuery.
Some notes:
In native JS you can add several css styles to element:
But you have some restriction:
style.cssText
overrides previous propertiesstyle
Jquery still rocks even in 2018
$ = q => [... document.querySelectorAll(q)]
Well, I think jquery Is still a valid option when you just want to make a simple website, which makes for 99% of the websites out there. There's no need to go react or vue if you're not really gonna be building an app-like kind of thing.
JQuery gets too much undeserved hate lately, because some people that got a sort of addiction to it and can't code 3 lines of JavaScript without it.
Sure, it was much more relevant in the dark ages of CSS 2 and crappy js, but even today, it's a valid tool for some works.
The key is to know when and why use each tool. (Which btw, the post makes an awful job at)
If all you got is a hammer, all tasks will look like nails.
Sometimes vanilla js will do, sometimes you need to go with a modern framework like vue or react, and sometimes the good ol' jquery will work just fine. Heck, nowadays most of what we used jquery for back in the day can now be achieved with CSS 3 and the simplest event listeners
I might not use it anymore , but I refuse to speak badly of a tool that provided us with such a great experience in the past. Which also means we might have to maintain legacy jquery code for a long, long time.
The controversy in the comments makes me laugh anytime someone thinks jQuery is dead. Of course there's specialized stuff that makes some of these things easier in some instances, and as JS evolves, it adds some of this, but it's not dead, and doesn't need to be.
Thanks for keeping this alive, because sometimes you don't need a whole framework to to the little things, and sometimes there's so much specialization that no one framework will cover it well.
Anyone who gets cranky about a tech. that other people are using is either posturing or doesn't know the whole story about an individual project. There's a lot of legacy stuff out there, including browsers that sometimes have to be targeted -- better than it used to be, but still an issue.
Yes, jQuery is getting slowly replaced, but there's no reason to stop showing people how to do the basics until they can pop a framework (sometimes you don't know the best one to use in a given instance) in or upgrade their legacy stuff. Sometimes you just need to know what the previous people did so you CAN replace it with newer ES versions or framework code. Sometimes you need to get it past an entrenched IT team that has whitelisted jQuery libs but not necessarily approve of the latest and greatest yet.
For those laughing at the last bit, or refusing that kind of work, I'm happy to collect your paycheck once I'm done with my stints doing data integration work that you're also refusing.
This is just terrifying on soo soo many levels. Next we going to have developers saying they need bootstrap to colour buttons
zamjs.com
No more long sentences from DOMapi
A Javascript developer needs jQuery like a fish needs a bicycle.
...I'm actually unsure if I believe this myself. I just REALLY wanted to write it.
jquery syntax is still useful for web scrapping i think.
I've added about the area of ββthis post.
I like how people grow up learning js with jquery say shit like dom operation cost and donβt use hide method.
Itβs like all the talents who had bought into web development at beginning love to use native method rather then jquery.
There are other advantages like jQuery caches the selectors.
You can also import only jQuery small functions and use those.
Ppl say we don't need jQuery anymore because we moved to webComponents, to use jQuery there is like playing God and is not good to overwrite the internal state of a comp from outside, aka react, vuejs.
I would still use jQuery for small static websites like landing pages or if I want to make a quick solution.
So you would rather load in 100kb of vanilla js, so that you can use $ instead of just using vanilla js... So glad I don't work with you...
You are clearly too narrow minded to have a conversation, so no use to reply, and you didn't even read my message.
I think the author didn't presented the advantages of jQuery (poor selection of examples), and by your example clearly you don't know what jQuery is composed of (the sizzle, selector is only a small part of it).
As a developer of almost 15 years, it's been at least 8 since I used jQuery, while it can potentially add value, that is far offset by the cost of its inclusion, 100% of which can very easily be done without it, if you are a competent developer.
If you start to do your own js, it will work in a very competent way, until a browser update with a new standard. If you use jQuery or other maybe you have only to update the library and not rework it for all diferent browsers. (sorry for my bad english)
Or more likely because you now have a hard dependacy on a boat load of code you never needed, that upgrade comes with breaking changes doing more harm and wasting more time than if you had just done it properly from the start.
Its your slow underperforming website, not mine.
Why use a sdk if you can write assembler code rigth?
Ya, you clearly do not understand cost value analysis, while I at no point said jQuery does not add value, in the vast majority of implications the cost far exceeds the value.
I write high performance we applications, up until the day I come across an application that will make 100% utilisation of every feature jQuery has, then all its doing is slowing down my application with code I DO NOT NEED AND CANNOT EXCLUDE.
Every application I wrote only ever has the code it needs, nothing more. Performance is king, no wasted wasted space, no unnecessary bloat.
I do understand actually, but since I am not programing a kernel module using jquery, I am ok with it.
You clearly haven't used a profiler to compare how a 40kb code that is not executed improves your performance. You just throwing big words or your websites has to run on fridges and TVs.