TExcelTAD
Home › jQuery › jQuery Intro

jQuery Introduction

jQuery is a small JavaScript library that makes everyday browser tasks short and readable: finding elements, reacting to events, changing content and styles, running animations and talking to a server. Its motto is "write less, do more" — a job that takes ten lines of plain DOM code often takes one line of jQuery.

jQuery was released in 2006, when every browser had its own quirks. It hid those differences behind one consistent API, and it became the most widely used JavaScript library on the web. Today browsers agree much more, and many jQuery ideas (like CSS-selector lookup) were added to the platform itself — but millions of sites, WordPress themes and admin panels still run on jQuery, so reading and writing it is a very practical skill.

What jQuery gives you

Area Example What it does
Selecting $(".card") Find elements with any CSS selector
Events $("button").on("click", fn) Attach handlers to many elements at once
Effects $("#box").fadeOut() Built-in show/hide/fade/slide/animate
DOM changes $("p").text("Hi") Read and write text, HTML, attributes, classes
Traversing $(this).closest("li") Move up, down and sideways in the tree
AJAX $.getJSON(url, fn) Load data without reloading the page
Utilities $.each, $.extend Helpers for arrays and objects

Your first jQuery page

The example below loads jQuery from its official CDN, then uses it. Press ▶ Run, click the button, and watch the console area under the preview.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    #greeting { padding: 12px; background: #e8f1fa; border-left: 4px solid #0769ad; }
  </style>
</head>
<body>
  <h2>Hello, jQuery!</h2>
  <p id="greeting">Click the button to change me.</p>
  <button id="go">Say hi</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#go").on("click", function () {
      $("#greeting").text("jQuery says hi! 👋");
      console.log("Button clicked, jQuery version", $.fn.jquery);
    });
  </script>
</body>
</html>

Read the script part aloud: "find #go, and on click, find #greeting and set its text." That readability is the core appeal of jQuery.

The same thing without jQuery

For comparison, this is the plain-JavaScript version. It is not much longer here, but the difference grows once you work with many elements, animations or AJAX.

js
document.getElementById("go").addEventListener("click", () => {
  document.getElementById("greeting").textContent = "Plain JS says hi!";
});

Working on many elements at once

A jQuery call returns a jQuery object — a list-like wrapper around zero or more elements. Most methods automatically apply to every element in the list, so you rarely need loops.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <style>
    body { font-family: sans-serif; }
    li { padding: 4px 8px; margin: 4px 0; border-radius: 4px; }
    .done { background: #c8f7c5; text-decoration: line-through; }
  </style>
</head>
<body>
  <ul>
    <li>Buy milk</li>
    <li>Walk the dog</li>
    <li>Learn jQuery</li>
  </ul>
  <button id="all">Mark all done</button>
  <button id="reset">Reset</button>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    $("#all").on("click", function () {
      $("li").addClass("done");          // applies to all three <li>
      console.log("Items marked:", $("li").length);
    });
    $("#reset").on("click", function () {
      $("li").removeClass("done");
    });
  </script>
</body>
</html>

$("li") matched three elements, and .addClass("done") changed all of them in one call.

Should you still learn jQuery?

  • Yes, to maintain existing code. A large share of the web — including WordPress, many Bootstrap 3/4 sites and internal tools — uses it.
  • Yes, as a gentle DOM introduction. jQuery's API teaches selecting, events and DOM changes with very little noise.
  • For new projects, consider plain JavaScript or a framework. Modern browsers provide querySelectorAll, classList, fetch and the Web Animations API. The last lessons of this course show the vanilla equivalents and how to migrate.

Note: This course uses jQuery 3.7.1, the current 3.x release. Code written for jQuery 3 also runs on jQuery 4 except for a few removed helpers, which are mentioned where relevant.

Test yourself

What does a call like $("li") return?

  1. A single DOM element
  2. A jQuery object wrapping all matching elements
  3. An array of strings
  4. undefined until the page loads

What happens when you call .addClass("done") on a jQuery object that contains three elements?

  1. Only the first element gets the class
  2. An error is thrown
  3. All three elements get the class
  4. Only the last element gets the class

Which statement about jQuery today is most accurate?

  1. Browsers cannot run jQuery anymore
  2. It is still widely used, but many of its features now exist natively in browsers
  3. It is required to use querySelectorAll
  4. It only works on servers