Introduction to jQuery
jQuery is a small JavaScript library that makes common browser tasks shorter to write: selecting elements, changing their content and styles, handling events, running simple animations and making AJAX requests. It wraps these jobs behind one function, $, and smooths over differences between browsers. Many existing websites, themes and plugins still use it, so reading and writing jQuery remains a useful skill.
What jQuery does for you
Compare a typical task in plain JavaScript and in jQuery: hide every element with the class notice.
// plain JavaScript
document.querySelectorAll('.notice').forEach((el) => {
el.style.display = 'none';
});
// jQuery
$('.notice').hide();
jQuery selects with CSS selectors, applies the action to every matched element automatically, and lets you chain several actions together. That "select, then act" style is its core idea.
Its main areas are DOM selection and traversal (finding parents, children and siblings), manipulation (changing text, HTML, attributes and classes), events, effects such as fadeIn and slideUp, and AJAX helpers like $.get and $.ajax. The following lessons go through these one by one.
Adding jQuery to a page
jQuery is a single JavaScript file. You can download it, install it with npm install jquery, or load it from a CDN. It must load before any script that uses $.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery demo</title>
</head>
<body>
<button id="toggle">Show details</button>
<p id="details" style="display: none;">Classes run on weekends.</p>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(function () {
$('#toggle').on('click', function () {
$('#details').slideToggle(200);
});
});
</script>
</body>
</html>
The $(function () { ... }) wrapper is shorthand for "run this when the DOM is ready". It ensures elements exist before your code looks for them. The older form, $(document).ready(...), does the same thing.
Full build versus slim build
jQuery 3 comes in two builds. The full build includes everything. The slim build leaves out the AJAX functions and the effects module, so methods like $.ajax and animations such as slideToggle are missing. If you copy a slim CDN tag and then call $.ajax, you get a "not a function" error. Pick the full build if you need either feature.
The "$ is not defined" error
This is the error beginners hit most. It nearly always means one of three things:
- Your script runs before the jQuery script tag, so
$does not exist yet. Put jQuery first. - The jQuery file failed to load, for example because of a typo in the path. Check the Network tab in developer tools.
- Your script uses the
deferormoduleattribute but jQuery does not, or the other way round, changing their order of execution.
Where jQuery fits today
Modern browsers now support many features that once needed jQuery, such as querySelectorAll, classList and fetch. New projects built with frameworks like React generally do not use it, and mixing jQuery DOM changes with React's rendering causes conflicts. jQuery still makes sense when maintaining existing sites, working with plugins that depend on it, or adding small interactions to simple pages.
Points to keep: load jQuery before your own scripts, wrap code in the ready handler, choose the full build when you need AJAX or effects, and think of every jQuery call as "select elements, then do something to them".
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.