Search on this site

Blog ✌️

Why you should ditch jQuery for native JavaScript
@danestves
--- views

Why you should ditch jQuery for native JavaScript

Published

Do you wish you could relive the days when jQuery ruled? Better get your applications on steroids if you want to stand a chance. jQuery is a cross-platform JavaScript library, initially created so that the code we wrote in Google Chrome could also work in Mozilla, Internet Explorer and all available browsers.

Remember that time when jQuery was the popular kid in school? 🀩 Well let me tell you, those times are long gone πŸ‘΄.

What is jQuery?

jQuery is a cross-platform JavaScript library initially created so that the code we wrote in Google Chrome could also work in Mozilla, Internet Explorer and all available browsers. But that was before, nowadays jQuery is not necessary for new projects, only for legacy projects.

I know you are going to tell me that there are still projects that use jQuery such as Bootstrap and WordPress 🀨, but there is news, recently Bootstrap announced that its version five will come completely in Vanilla JavaScript, this means that they will no longer need jQuery in their dependencies. And as for WordPress πŸ€” no one knows yet, but what is certain is that they are opting for new technologies such as React in their block editor Gutenberg.

You might not need jQuery

You might not need jQuery is a site I like to visit when there are things I previously did with jQuery and I want to know how they would be done with Vanilla JavaScript, remember that jQuery is just JavaScript interpreted for cross-browser compatibility.

The animations that we do with .fadeIn() could now comfortably be done with a CSS class and do something like this:

1const photo = document.getElementById('myPhoto')
2Β 
3photo.classList.add('show')
4photo.classList.remove('hide')
1.show {
2 transition: opacity 400ms;
3}
4Β 
5.hide {
6 opacity: 0;
7}

With this we already have animations adding interactivity using JavaScript and CSS together.

Do you miss the jQuery selector?

Many people used to use jQuery mostly because of its selector that attracted a lot of attention, you could select any element of the document just using $('.myClass #myId h1'), and it is true, it is very cool this way and I am going to show you how πŸ₯³.

  1. Let's create our JavaScript with the selector we want to perform:
1const $ = document.querySelector.bind(document) // Create a selector that references the entire current document
  1. We create an HTML with attributes that we can handle:
1<!DOCTYPE html>
2<html>
3 <head>
4 <title>My Selector like jQuery</title>
5 </head>
6 <body>
7 <h1>h1 Title</h1>
8 <p class="class-p">with class</p>
9 </body>
10</html>
  1. Now we can change properties of the elements in the following way:
1const $ = document.querySelector.bind(document)
2Β 
3$('body').style.background = 'red'
4$('body').style.color = '#fff'
5$('h1').style.fontSize = '200%'
6$('.class-p').style.fontSize = '48px'
7Β 
8// And whatever you need to do when you call the element(s)
  1. That's it, congratulations! You've just made your own jQuery-style selector with Vanilla JavaScript πŸŽ‰πŸ₯³.

Final result

How to keep learning?

I did a course on Platzi on how to move from jQuery to JavaScript that helped me to update myself that I really don't need to use this library anymore, unless of course I work on legacy projects. I'll leave you with this list of resources that will help you better understand how to move from jQuery to JavaScript:

Conclusions

Finally I recommend you to learn little by little the new trends that are coming out in the market and adapt to their use. I'm not saying that you should follow the popularity of today's frameworks and libraries that are currently used, experiment, research, practice a lot and above all never stop learning 😎