Introduction to jQuery
Before you write a single line of jQuery, it helps to understand what it actually is, why it exists, and whether it still makes sense to learn in 2026. This lesson answers all of that — honestly.
In This Lesson
What is jQuery?
jQuery is a JavaScript library created by John Resig in 2006. That's it — it's a file of JavaScript code that adds shorter, simpler ways to do things you'd otherwise write in longer vanilla JavaScript.
Its whole point is captured in four words: "Write Less, Do More." Things that take 5 lines of vanilla JavaScript take 1 line of jQuery. That gap has closed over time with modern JavaScript, but jQuery still wins on readability and browser consistency.
jQuery runs on hundreds of millions of websites — including WordPress, which powers over 40% of the internet and ships jQuery by default. Even if you move on to React or Vue later, you'll still encounter jQuery in real projects.
Why Use jQuery?
Here are the six practical reasons developers still reach for jQuery:
What jQuery Can Do
jQuery covers five main areas. Each one simplifies a different part of working with web pages:
DOM Manipulation
$("#myEl").html("New content"); // Set HTML $("#myEl").text("Plain text only"); // Set text $("#myEl").append("Added at end"); // Append content $("#myEl").remove(); // Delete element
Effects & Animation
$("#box").hide(); // Instant hide $("#box").fadeOut(600); // Fade out over 600ms $("#box").slideDown(); // Slide open animation
jQuery vs Vanilla JavaScript — The Real Difference
// Change color of ALL paragraphs — vanilla JS const paras = document.querySelectorAll("p"); paras.forEach(p => p.style.color = "red"); // Same thing in jQuery — one line $("p").css("color", "red");
jQuery Versions
There are three major version families. For any new project, use jQuery 3.x — it's the only one still actively maintained:
Adding jQuery to Your Page
The easiest way is the CDN link — no download, one line, works immediately. Put it just before the closing </body> tag, then put your own code in a second script tag after it:
<body> <!-- your HTML content --> <p>Hello!</p> <button>Click me</button> <!-- Step 1: jQuery first --> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <!-- Step 2: Your code second --> <script> $(function() { $("button").click(function() { $("p").hide(); }); }); </script> </body>
Document Ready — Always Use This
Wrap all your jQuery inside $(function(){...}). This makes sure the page has fully loaded before jQuery tries to find any elements. Without it, your selectors find nothing because the HTML hasn't been parsed yet.
// Full version $(document).ready(function() { // all your jQuery here }); // Shorthand — same thing, most common $(function() { // all your jQuery here });
Basic Syntax — One Pattern, Everything
Every jQuery statement follows the same three-part structure:
| Part | What it does | Example |
|---|---|---|
| $ | The jQuery function — gives you access to everything | $() |
| selector | Finds HTML elements using CSS syntax | "#id", ".class", "p" |
| action() | The jQuery method to run on those elements | .hide(), .css(), .click() |
jQuery vs Vanilla JavaScript
| Task | Vanilla JS | jQuery |
|---|---|---|
| Select by ID | document.getElementById("id") | $("#id") |
| Select by class | querySelectorAll(".class") | $(".class") |
| Hide element | el.style.display = "none" | $(sel).hide() |
| Add event | el.addEventListener("click", fn) | $(sel).click(fn) |
| Browser compat | Manual handling needed | Automatic |
| File size | Zero overhead | ~30KB minified |
Pros & Cons
- Much shorter code than vanilla JS
- Cross-browser compatibility handled
- Beginner-friendly — CSS selectors you know
- Built-in animations and effects
- Massive community and plugin library
- Used by WordPress — very employable
- Adds ~30KB to page load
- Slower than native JavaScript
- Not ideal for large single-page apps
- Modern JS has reduced the gap
- Less relevant for framework-based projects
Your First jQuery Page
Copy this, open it in a browser, click the paragraph — it fades out. Click the button — it comes back. That's jQuery in action:
<!DOCTYPE html> <html> <body> <h1>Hello jQuery!</h1> <p>Click me to fade out.</p> <button id="showBtn">Bring it back</button> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(function() { // Click the paragraph to fade it out $("p").click(function() { $(this).fadeOut(600); }); // Button brings it back $("#showBtn").click(function() { $("p").fadeIn(600); }); }); </script> </body> </html>
Practice
Questions About jQuery
Yes — not because it's the future, but because it's the present on a huge number of sites. WordPress alone powers 40%+ of the web and depends on jQuery. If you ever work with legacy codebases, client sites, or WordPress themes, jQuery will come up. It's also one of the most approachable ways to learn DOM manipulation concepts that transfer directly to vanilla JavaScript and modern frameworks.
Almost certainly because your code is running before jQuery has loaded. Check two things: first, that your jQuery CDN script tag is actually in your HTML. Second, that your own script tag comes after the jQuery script tag, not before. The order matters — jQuery must exist before you try to use it.
The document ready function $(function(){...}) ensures your code runs only after the HTML has fully loaded and the browser has built the DOM. Without it, jQuery tries to find elements before they exist and finds nothing. If you put your script tag at the bottom of the body, just before the closing tag, the DOM is already loaded by that point — but using document ready is still the safe habit to build.
Learn jQuery first if you're a beginner — it teaches you how the browser DOM actually works, how events fire, and how AJAX requests work. These are foundational concepts that make React or Vue much easier to understand later. Jumping to React without that foundation often leads to using it without really understanding what's happening underneath.
Nothing at all — they're exactly the same. $ is just a shorthand alias for jQuery. Writing $("p").hide() and jQuery("p").hide() do identical things. Developers use $ because it's shorter. If you ever run into a conflict where another library also uses $, call jQuery.noConflict() to release the shorthand and use the full jQuery() instead.