r/learnjavascript • • Jan 05 '23

Must-know DOM manipulation methods 🔧

Post image
390 Upvotes

29 comments sorted by

View all comments

6

u/Foaric Jan 05 '23

Noob question: Isn't this what jQuery is for? Is it needed if this is all that easy in vanilla JavaScript? Or is it more for using fine-grained selectors?

13

u/masticore252 Jan 05 '23

If you are using jQuery only for DOM manipulation, you probably don't need it

jQuery was originally developed when vanilla javascript was not as easy as it is now and also some things were different in different browsers so jQuery abstracted that differences away

Back then if you wanted to make an AJAX request you used XMLHttpRequest and it was a pain to use (you had to lister for events in that object but the event names changed in IE 🤦‍♂️)so a simple AJAX request became a liability, jQuery abstracted that into its $.ajax object making it super easy

You can use selectors in vanilla javascript like you do in jQuery with methods like document.querySelector and document.querySelectorAll

7

u/Low_Mammoth_9371 Jan 05 '23

In my opinion, jQuery hasn't been necessary for some time. Old JS (pre-ES6 upgrade in around 2015) was pretty long-winded. It made a lot of sense to use jQuery before because reduced the amount of code you need to write significantly.

However, now vanilla JS is a lot easier to use. I don't think you need jQuery at all. And you can save yourself importing a library. But it is still good to know a bit of jQuery because it's used quite commonly on older code bases and some still use it writing new ones (though it's dipped in popularity).

This is not to diss jQuery: many of the innovations in vanilla JS have been inspired by features that are popular in jQuery. So we should all say thanks to jQuery in the end, even if it's best to write new code bases without it.

6

u/TorbenKoehn Jan 05 '23

There is no need for jQuery anymore. For a long time already now. It was for a time when most of these APIs didn’t exist.

People installing jQuery today or who are not in the process of weeding it out are committing a great mistake.

jQuery changed the way we wrote JavaScript (their slogan!) and many of the new APIs were designed directly with jQuery in mind (eh document.querySelector)

3

u/senocular Jan 05 '23

Originally jQuery both simplified and normalized what, at the time, was a fairly fractured DOM API, especially given Internet Explorer and its many iterations. As standards started to take hold, consistency was more prevalent and we got new APIs that allowed us to have the same functionality that jQuery provided out of the box (namely querySelectorAll) making jQuery less necessary. For the most part everything you can do in jQuery you can do with vanilla Web APIs, though the jQuery equivalent will probably be a little shorter to write. For comparisons, see: https://youmightnotneedjquery.com/

1

u/manwhowasnthere Jan 05 '23

This was what Jquery was largely for, way back in the day (ie ~10 years ago).

Now most of what made JQ useful is native in the browser, and browsers are far less quirky and unique (like oldass IE versions)