Open In App

How to add jQuery code to HTML file ?

Last Updated : 31 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to add jQuery code to an HTML file. You can easily add jQuery to HTML by using several methods, like adding jQuery from CDN or directly downloading jQuery files and including them in your projects. Several methods are discussed in this article.

Methods:

  • Download and include the jQuery file
  • Including jQuery from CDN.

Approach1: Download and include the jQuery file

Follow the information below to include jQuery in your HTML file.

  1. Use this link to download the jQuery file from the official jQuery website. ( download compressed or uncompressed files according to your need).
  2. After downloading, just move the downloaded file into the HTML file to which you want to add your jQuery.
  3. Finally, use the below syntax to include jQuery in the HTML file. i.e. add jQueryfile name between script tags.
<script type="text/javascript" src="jquery-3.5.1.min.js"> </script>

Example: In this example, we will use the above approach

HTML




<!DOCTYPE html>
<html>
<head>
    <script src=
    </script>
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("h2").html("Complete Portal for Geeks</b>");
            });
        });
    </script>
</head>
<body>
    <center>
        <h2>GeeksforGeeks</h2>
        <button>Click here</button>
    </center>
</body>
</html>


Output:

Approach 2: Follow the step below to include jQuery in the  HTML  file.

  1. In this, you just need to add the link below with the script tag to your  HTML  file, whether CDN is provided by Google or Microsoft.
    • Google CDN

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js”></script>

  • Microsoft CDN

<script src=’http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js’></script>

Example: In this example, we will use the above approach.

HTML




<html>
 
<head>
    <script src=
    </script>
    <title>Added Jquery into HTML</title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <center>
        <h1>
              Welcome to GeeksforGeeks
          </h1>
        <button id="trigger">
              Click me
          </button>
        <h3 id="demo"></h3>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#trigger").click(function () {
                    $("#demo").html("Complete portal for Geeks");
                });
            });
        </script>
    </center>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments