TExcelTAD
Home › jQuery › jQuery Get Started

jQuery Get Started

jQuery is a single JavaScript file. To use it you load that file in your page before any script that uses $. There are two common ways: from a CDN, or from your own server (often installed with npm).

Option 1: load it from a CDN

A CDN (content delivery network) hosts the file for you on fast servers around the world. The official one is code.jquery.com:

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

Other popular CDNs host the identical file:

CDN URL
jQuery CDN https://code.jquery.com/jquery-3.7.1.min.js
cdnjs https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js
jsDelivr https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js

Tip: Always pin an exact version (3.7.1, not "latest"). A surprise major update could break your site overnight.

Option 2: download or install it

You can download jquery-3.7.1.min.js from jquery.com and serve it yourself, or install it with npm in a bundled project:

bash
npm install jquery@3.7.1
js
// in a bundled project (Vite, webpack, …)
import $ from "jquery";
$("h1").text("Loaded through npm");

Minified vs. uncompressed

File Size (approx.) Use it for
jquery-3.7.1.min.js ~87 KB (~30 KB gzipped) Production — whitespace and long names removed
jquery-3.7.1.js ~285 KB Reading or debugging the source
jquery-3.7.1.slim.min.js ~70 KB Smaller build without AJAX and effects

Note: The slim build has no $.ajax, no .fadeIn()/.animate(). If you use those, load the full build.

Checking that jQuery is loaded

Once the file runs, it defines two global names: jQuery and its short alias $. You can ask for the version with $.fn.jquery.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p id="status">Checking…</p>

  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    if (window.jQuery) {
      $("#status").text("jQuery " + $.fn.jquery + " is ready ✔").css("color", "green");
      console.log("$ === jQuery:", $ === jQuery);
    } else {
      document.getElementById("status").textContent = "jQuery did not load";
    }
  </script>
</body>
</html>

Script order matters

Scripts run in the order they appear. If your code comes before the jQuery tag, $ does not exist yet and you get ReferenceError: $ is not defined — the single most common jQuery error.

html
<!-- ❌ wrong: $ is used before jQuery is loaded -->
<script>$("p").hide();</script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<!-- ✅ right -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>$("p").hide();</script>

Where to put the tags

Your code also needs the HTML elements to exist before it selects them. Two good patterns:

  1. Put the scripts at the end of <body> (used throughout this course).
  2. Put them in <head> and wrap your code in a ready handler, which runs once the document has been parsed:
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <script>
    // Runs after the HTML below has been parsed
    $(function () {
      $("#msg").text("The DOM is ready, so #msg exists.");
      console.log("paragraphs found:", $("p").length);
    });
  </script>
</head>
<body style="font-family: sans-serif">
  <p id="msg">Waiting…</p>
  <p>Another paragraph.</p>
</body>
</html>

Try deleting the $(function () { … }) wrapper (keep the two inner lines) and run again: nothing changes on the page and the log says 0, because the script ran before the paragraphs existed.

Integrity and crossorigin

The jQuery CDN offers a Subresource Integrity hash. The browser refuses to run the file if its content does not match the hash, which protects you if the CDN is ever compromised:

html
<script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
        crossorigin="anonymous"></script>

Copy the hash from the official download page rather than typing it by hand.

Test yourself

You see ReferenceError: $ is not defined. What is the most likely cause?

  1. The selector is wrong
  2. Your script runs before the jQuery file is loaded
  3. The element has no id
  4. You used single quotes

Which build should you avoid if your code uses .fadeIn() or $.ajax()?

  1. jquery-3.7.1.min.js
  2. jquery-3.7.1.js
  3. jquery-3.7.1.slim.min.js
  4. The npm package

How can you read the loaded jQuery version?

  1. $.version()
  2. $.fn.jquery
  3. jQuery.v
  4. $("version")