Learn how to Convert jQuery to Vanilla JavaScript

Muhaymin Bin Mehmood

Muhaymin Bin Mehmood

·
3 min read
Published Sep 26, 2024
Updated about 1 year ago
How to Convert jQuery to Vanilla JavaScript Banner Image
16 min read
376 words
10 sections28 code blocks

JavaScript, a powerful programming language, is widely used for creating dynamic web applications. While jQuery once simplified JavaScript for developers by offering concise syntax, modern JavaScript (ES6+) now includes many native features that make jQuery less necessary. In this blog, you'll learn how to convert common jQuery code snippets into vanilla JavaScript, making your code lighter, faster, and easier to maintain.

1. Element Selection

In jQuery, selecting elements is incredibly easy using the $() syntax, but you can achieve the same results in JavaScript with methods like querySelector() and querySelectorAll().

jQuery:

JavaScript
$(".my-class");  // Selects all elements with class "my-class"

Vanilla JavaScript:

JavaScript
document.querySelector(".my-class");  // Selects the first element with class "my-class"
document.querySelectorAll(".my-class");  // Selects all elements with class "my-class"


2. Selecting HTML Body and Head

You can directly reference the body and head in JavaScript without a special selector.

jQuery:

JavaScript
$("body");

Vanilla JavaScript:

JavaScript
document.body;

3. Class Manipulation

Manipulating classes in jQuery is simple, but JavaScript's classList API provides similar functionality.

  • Adding a Class

jQuery:

JavaScript
$(".my-class").addClass("new-class");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-class").classList.add("new-class");
  • Removing a Class

jQuery:

JavaScript
$(".my-class").removeClass("old-class");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-class").classList.remove("old-class");
  • Toggling a Class

jQuery:

JavaScript
$(".my-class").toggleClass("active");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-class").classList.toggle("active");
  • Checking for a Class

jQuery:

JavaScript
$(".my-class").hasClass("active");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-class").classList.contains("active");

4. Event Listeners

In jQuery, adding event listeners is straightforward, but JavaScript’s addEventListener() provides a more flexible solution.

jQuery:

JavaScript
$(".btn").click(function() {
  alert("Button clicked!");
});

Vanilla JavaScript:

JavaScript
document.querySelector(".btn").addEventListener("click", function() {
  alert("Button clicked!");
});

5. CSS Manipulation

You can manipulate CSS in both jQuery and JavaScript, though the syntax differs slightly.

  • Changing CSS Properties

jQuery:

JavaScript
$(".my-div").css({ "margin": "20px" });

Vanilla JavaScript:

JavaScript
document.querySelector(".my-div").style.margin = "20px";

6. Handling HTML Content

When it comes to modifying the HTML content of an element, both jQuery and JavaScript offer methods to achieve this.

  • Get or Set HTML Content

jQuery:

JavaScript
$(".my-div").html("<p>New content</p>");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-div").innerHTML = "<p>New content</p>";
  • Get or Set Text Content

jQuery:

JavaScript
$(".my-div").text("New text content");

Vanilla JavaScript:

JavaScript
document.querySelector(".my-div").textContent = "New text content";

7. AJAX Requests

While jQuery simplifies AJAX requests, the fetch() API in JavaScript is now the standard for making asynchronous HTTP requests.

jQuery:

JavaScript
$.ajax({
  url: "https://api.example.com/data",
  method: "GET",
  success: function(response) {
    console.log(response);
  }
});

Vanilla JavaScript:

JavaScript
fetch("https://api.example.com/data")
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

8. Animations

jQuery has built-in methods like fadeIn() and slideToggle(), but you can replicate these animations in JavaScript using CSS transitions or JavaScript’s animate() API.

jQuery:

JavaScript
$(".my-div").fadeIn();

Vanilla JavaScript:

JavaScript
document.querySelector(".my-div").style.opacity = 1;
document.querySelector(".my-div").style.transition = "opacity 0.5s";

9. DOM Traversal

Traversing the DOM in jQuery is made simple with methods like parent() or find(). JavaScript offers similar functionality through parentElement and querySelector().

  • Finding a Parent Element

jQuery:

JavaScript
$(".child").parent();

Vanilla JavaScript:

JavaScript
document.querySelector(".child").parentElement;
  • Finding a Child Element

jQuery:

JavaScript
$(".parent").find(".child");

Vanilla JavaScript:

JavaScript
document.querySelector(".parent .child");

10. Conclusion

While jQuery was revolutionary, modern JavaScript now includes many of the features that once made jQuery essential. By switching from jQuery to vanilla JavaScript, your code becomes lighter, faster, and more compatible with modern web development standards.

No comments yet. Be the first to comment!

Leave a Comment

2000 characters remaining

Related Articles

View all
Muhaymin Bin Mehmood

About Muhaymin Bin Mehmood

Front-end Developer skilled in the MERN stack, experienced in web and mobile development. Proficient in React.js, Node.js, and Express.js, with a focus on client interactions, sales support, and high-performance applications.