jQuery is a fast, small, and feature-rich JavaScript library. Before using jQuery features like selectors, events, and animations, you must properly install or include jQuery in your project.
To use jQuery, you include the jQuery file inside a <script> tag. Once included, you can use the $ symbol to access jQuery functions.
A crucial part of jQuery installation is ensuring the library and the HTML content are loaded before running scripts. The standard syntax wraps code like this:
$(document).ready(function() {
// jQuery methods go here...
});
// Including jQuery using CDN
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
// Including jQuery from local file
<script src="js/jquery.min.js"></script>
// Terminal command for NPM
npm install jquery
// Terminal command for Yarn
yarn add jquery
// Import in your JS file
import $ from "jquery";
When jQuery is correctly installed, the browser loads the library first. After that, any jQuery code written in your script will execute without errors.
Below is a Live Demo. This page actually has jQuery loaded in the background. Click the button below to test if it detects the library.
Click the button to run a real jQuery script:
// HTML
<button id="runCheckBtn">Run jQuery Check</button>
<div id="demoStatus">Waiting...</div>
// jQuery Script
$(document).ready(function(){
$("#runCheckBtn").click(function(){
$("#demoStatus")
.text("✅ jQuery is installed and active!")
.css({"color": "green", "border-color": "green"});
});
});
// Check if jQuery is loaded successfully
$(document).ready(function(){
alert("jQuery is working!");
});
$(document).ready() (or the shorthand $(function(){...})) to ensure the DOM is safe to manipulate.$().jquery to see the installed version.