← Back to Chapters

jQuery Installation

⚙️ jQuery Installation

? Quick Overview

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.

? Key Concepts

  • jQuery is a JavaScript library, not a framework
  • It must be loaded before writing jQuery code
  • Can be installed via CDN or local file
  • Works in all modern browsers

? Syntax / Theory

To use jQuery, you include the jQuery file inside a <script> tag. Once included, you can use the $ symbol to access jQuery functions.

Why use $(document).ready()?

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...
});

? Code Example(s)

? View Code Example (CDN Installation)
// Including jQuery using CDN
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
? View Code Example (Local Installation)
// Including jQuery from local file
<script src="js/jquery.min.js"></script>
? View Code Example (NPM/Yarn for Modern Projects)
// Terminal command for NPM
npm install jquery

// Terminal command for Yarn
yarn add jquery

// Import in your JS file
import $ from "jquery";

? Live Output / Explanation

Explanation

When jQuery is correctly installed, the browser loads the library first. After that, any jQuery code written in your script will execute without errors.

? Interactive Example

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.

jQuery Status Check

Click the button to run a real jQuery script:

Status: Waiting for input...
? View Source Code for the Demo above
// 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"});
  });
});
 
? View Code Example (Basic Alert Test)
// Check if jQuery is loaded successfully
$(document).ready(function(){
  alert("jQuery is working!");
});

? Use Cases

  • Quick DOM manipulation
  • Handling events easily
  • Animations and effects
  • AJAX requests

? Tips & Best Practices

  • Always include jQuery before your custom script
  • Use CDN for faster loading in production
  • Keep local copy as backup if CDN fails
  • Always wrap your logic in $(document).ready() (or the shorthand $(function(){...})) to ensure the DOM is safe to manipulate.

? Try It Yourself

  • Install jQuery using CDN and test an alert
  • Download jQuery and link it locally
  • Check browser console (F12) and type $().jquery to see the installed version.