jQueryadvanced 8 min
Side-by-side comparisons: what jQuery did, what plain JavaScript does today, and when each makes sense.
How common jQuery tasks look in modern plain JavaScript, and how to decide which to use in a project.
jQuery was created in 2006 to smooth over big differences between browsers and to make the DOM less painful. Today browsers agree with each other and have the features built in. You will still meet jQuery in older sites, WordPress themes and plugins, so you need to read it, but for new code you often will not need it.
Modern browsers provide direct equivalents:
| Task | jQuery | Plain JavaScript |
|---|---|---|
| Select | $(".item") | document.querySelectorAll(".item") |
| One element | $("#a") | document.querySelector("#a") |
| Text | $(el).text("x") | el.textContent = "x" |
| Add class | $(el).addClass("on") | el.classList.add("on") |
| Event | $(el).on("click", fn) | el.addEventListener("click", fn) |
| Hide | $(el).hide() | el.style.display = "none" |
| Fetch JSON | $.getJSON(url) | fetch(url).then(r => r.json()) |
| Ready | $(function () {}) | document.addEventListener("DOMContentLoaded", fn) |
Reasons to keep jQuery:
Reasons to choose plain JavaScript:
The same behaviour written both ways: clicking a button changes a message and toggles a class.
on class.Rewrite this jQuery line in plain JavaScript: $(".item").css("color", "red");. Hint: loop over querySelectorAll and set el.style.color.
You finished the jQuery track. Try the practice exercises to lock it in.
<style>
.on { color: tomato; font-weight: bold; }
</style>
<button id="jq">jQuery version</button>
<button id="js">Plain JS version</button>
<p id="msg">Waiting...</p>
<script>
// jQuery
$("#jq").on("click", function () {
$("#msg").text("Clicked with jQuery").toggleClass("on");
});
// Plain JavaScript
document.querySelector("#js").addEventListener("click", function () {
const msg = document.querySelector("#msg");
msg.textContent = "Clicked with plain JS";
msg.classList.toggle("on");
});
</script>