HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

jQuery Introduction

jQuery is one of the world's most widely used JavaScript libraries. With a small amount of code, jQuery lets you accomplish tasks that would normally require many lines of plain JavaScript — making your development faster, cleaner, and more readable.


What is jQuery?

jQuery is a fast, lightweight, and feature-rich JavaScript library. It was created by John Resig in 2006 with a simple goal: "Write less, do more."

At its core, jQuery is a .js file that you include in your HTML page. Once included, it gives you access to a large collection of ready-made functions that help you:

  • Select and manipulate HTML elements with ease
  • Handle user events like clicks, keypresses, and form submissions
  • Create animations and visual effects
  • Send and receive data from a server without refreshing the page (AJAX)
  • Work consistently across all major browsers
Note: jQuery is not a replacement for JavaScript — it is JavaScript. It is simply a library built on top of JavaScript to make common tasks easier and shorter.

Why jQuery? The Core Idea

Plain JavaScript (also called Vanilla JS) is powerful but can be verbose. jQuery wraps many of those repetitive tasks into short, readable commands.

Here is a side-by-side comparison to show the difference:

Vanilla JavaScript
document
 .getElementById("btn")
 .addEventListener(
  "click", function() {
    document
    .getElementById("msg")
    .style.display = "none";
 }
);
jQuery
$("#btn").click(function() {
  $("#msg").hide();
});

Both examples do exactly the same thing — hide an element when a button is clicked. But look how much shorter and cleaner the jQuery version is!


Key Features of jQuery

Here is a quick overview of what jQuery offers out of the box:

🔍
DOM Selection
Easily find and work with HTML elements using CSS-style selectors.
✏️
DOM Manipulation
Change content, attributes, and styles of any element on the page.
🖱️
Event Handling
Respond to user actions like clicks, hovers, and keystrokes easily.
Animations
Fade, slide, and animate elements without writing complex CSS or JS.
🌐
AJAX Support
Fetch data from servers in the background — no page reload needed.
🔌
Plugin System
Thousands of free plugins extend jQuery for sliders, modals, and more.

How to Add jQuery to Your Page

There are two common ways to include jQuery in your HTML project:

Method 1: Using a CDN (Recommended for Beginners)

A CDN (Content Delivery Network) hosts jQuery on fast servers around the world. You simply add a <script> tag pointing to it — no download needed.

<!-- Add this just before the closing </body> tag -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
Tip: Always place your jQuery <script> tag before your own custom JavaScript files. jQuery must load first so its functions are available when your code runs.

Method 2: Downloading jQuery Locally

You can also download the jQuery file from jquery.com and host it yourself. This is useful when working offline or in environments with restricted internet access.

<!-- Link to your locally downloaded jQuery file -->
<script src="js/jquery-3.7.1.min.js"></script>

Your First jQuery Code

Almost every jQuery script you write will be wrapped inside a special function called the document ready handler. This makes sure your jQuery code only runs after the entire HTML page has loaded.

$(document).ready(function() {
    // Your jQuery code goes here
    // This runs after the page is fully loaded
});

There is also a shorter, modern version of the same thing:

$(function() {
    // Shorter version of document ready
});
Important: The $ symbol is the heart of jQuery. It is a shorthand for the jQuery function. Whenever you see $, think: "jQuery is at work here."

A Complete Working Example

Let's put everything together in one simple, working HTML page. When the user clicks the button, a paragraph will be hidden:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My First jQuery Page</title>
</head>
<body>

    <h2>Hello jQuery!</h2>
    <p id="message">Click the button below to hide me.</p>
    <button id="hideBtn">Hide the Paragraph</button>

    <!-- jQuery CDN -->
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

    <!-- Your jQuery code -->
    <script>
        $(document).ready(function() {
            $("#hideBtn").click(function() {
                $("#message").hide();
            });
        });
    </script>

</body>
</html>

Here is what the code does, line by line:

  • $(document).ready() — waits for the page to fully load before running any code.
  • $("#hideBtn") — selects the element with id="hideBtn" (the button).
  • .click(function() { ... }) — runs the code inside when the button is clicked.
  • $("#message").hide() — selects the paragraph and hides it.

Is jQuery Still Worth Learning?

You might have heard that modern frameworks like React and Vue have replaced jQuery. While those frameworks are popular for building complex single-page applications, jQuery is still widely used and relevant for several reasons:

  • It powers millions of websites — jQuery is used by more than 70% of the top websites on the internet, including WordPress sites.
  • It is simpler for smaller projects — For adding interactive features to a regular website, jQuery is often faster and easier than setting up a full framework.
  • It is beginner-friendly — Its syntax is intuitive and the learning curve is much gentler than modern frameworks.
  • It is part of many legacy systems — As a developer, you will likely encounter jQuery in existing projects and need to understand it.
  • Great plugin ecosystem — Countless plugins (sliders, date pickers, modals, etc.) are built on jQuery and still actively used.
Bottom line: jQuery is a fantastic tool to learn. It deepens your understanding of JavaScript, helps you work on existing projects, and lets you add powerful features to websites quickly and cleanly.

Key Points to Remember

  • jQuery is a JavaScript library, not a separate language — it is pure JavaScript under the hood.
  • The $ symbol is jQuery's shorthand function — it is how you access everything jQuery offers.
  • Always include jQuery before your own scripts, and wrap your code in $(document).ready().
  • jQuery uses CSS-style selectors to find elements — if you know CSS selectors, you already know half of jQuery.
  • jQuery version 3.x is the current recommended version for new projects.