- Home
- Getting Started
Getting started with jQuery
A short path from an empty page to a working script. Each step explains the reason before the code, so the pieces stay connected.
jQuery is a single JavaScript library, so the setup is small: decide where the file comes from, add it before your own script, and wait until the document is ready. The notes below use the official distribution.
Choose a delivery method
There are three common ways to get the library into a page. A public CDN is the quickest for experiments, a package manager suits a build process, and a local copy gives a team full control over versioning.
- Public CDN: load a hosted copy from the official jQuery CDN. This is the fastest route for a prototype.
- Package manager: install the
jquerypackage with npm when the project already has a build step. - Local copy: download the production file and serve it from your own assets folder.
Sources jQuery download page npm package official CDN npm documentation
Add the script
Place the jQuery script before your own script, so the $ shorthand exists by the time your code runs. The example below uses the official CDN and a local application file.
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="/assets/js/app.js"></script>
If the two files are reversed, your code may run before the library defines $. The page will report an error even though nothing is wrong with the library itself.
Reference MDN script element reference
Wait for the document
Wrap the first script in the document ready pattern. The callback runs after the page structure exists, which means selectors can find the elements they expect.
$(function () {
// The page is ready for selection
$('.note').addClass('isReady');
});
Reference jQuery ready documentation
Select and act
Selectors use the same patterns as CSS. A class selector finds every matching element, an ID selector finds one, and a method changes the result. Chaining keeps the steps together.
$('.card')
.text('Updated')
.addClass('isOpen');
Reference MDN querySelector reference
Handle events
Use .on() to attach a handler. The handler receives the event, and this refers to the element that triggered it. Delegation to a parent also covers elements added later.
$('.action').on('click', function () {
$(this).toggleClass('isOpen');
});
Request data
The shorthand helpers make a small request readable. The callback runs when the response arrives, and only the target region needs to change.
$.get('/data.json', function (data) {
$('#result').text(data.message);
});
07Before you ship
- The library loads before your own script, and the network panel shows no failed request.
- Your first selector runs inside the ready callback, not before the document exists.
- The console reports no missing function or undefined variable.
- The page still works when a request fails, because the error path has a message.
The learning center is the best next stop for guided tutorials, while the API documentation answers specific questions about a method. Both are linked from the resource shelf.
Go deeper on the functions
Each of the nine core functions has a short explanation, a code sample and an official reference.