Getting Started Lesson 2 of 10
Getting Started with jQuery — Adding It and document ready
Add jQuery to a page from a CDN or a downloaded file, run code when the page is ready with $(function), and avoid conflicts and loading-order mistakes.
jQuery is a single JavaScript file. Add it to your page, then write your own code after it.
From a CDN
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My jQuery Page</title>
</head>
<body>
<h1>Hello!</h1>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
The jQuery file must come before any script that uses $. Putting both just before </body> is the common choice.
Downloaded file (works offline)
Download the "compressed, production" file from jquery.com, save it in your project (e.g. js/jquery.min.js) and link it:
<script src="js/jquery.min.js"></script>
This is handy in school computer labs without reliable internet.
Waiting for the page: document ready
If your script is in the <head>, the elements don't exist yet when it runs. Wrap your code so it runs after the page is ready:
<script src="/vendor/jquery.min.js"></script>
<script>
$(function () {
// This runs after the HTML below has loaded
$("#msg").text("✅ jQuery is ready, and the page is loaded.");
});
</script>
<p id="msg">Loading…</p>
$(function () { ... }) is short for $(document).ready(function () { ... }). Even though this script is above the paragraph, it works because jQuery waits for the page.
Checking that jQuery works
<script src="/vendor/jquery.min.js"></script>
<p id="v"></p>
<script>
$("#v").text("jQuery version: " + $.fn.jquery);
</script>
Your own code in a separate file
// js/app.js
$(function () {
$(".alert .close").on("click", function () {
$(this).closest(".alert").fadeOut();
});
});
$ conflicts and noConflict
Some other libraries also use $. In WordPress, jQuery runs in "no conflict" mode, so $ isn't available directly. Use jQuery or wrap your code like this:
jQuery(function ($) {
// inside here, $ means jQuery again
$(".menu").slideDown();
});
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04