Add jQuery
CDN (easiest):
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
Download: save jquery-3.7.1.min.js in your project (for example public/js/) and link it. Use the .min (minified) file on live sites.
Place the jQuery script before your own scripts — your code needs $ to exist.
Document ready
Run code only after the HTML is loaded:
$(function () {
// your code here
});
This is the short form of $(document).ready(function () { ... });.
$ and jQuery
$ is just a short name for jQuery. If another library also uses $, write jQuery('.box') instead.
Example
Example — jQuery starter page
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Starter</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<h1>jQuery is ready?</h1>
<p id="status">Checking...</p>
<script>
$(function () {
$('#status').text('Yes! jQuery version ' + $.fn.jquery + ' is loaded.');
});
</script>
</body>
</html>