Cover Image for How to add jQuery to Html
72 views

How to add jQuery to Html

To add jQuery to an HTML document, you can include the jQuery library by following these steps:

  1. Download jQuery: Visit the official jQuery website (https://jquery.com/) and click on the “Download” button to get the latest version of jQuery. Alternatively, you can use a CDN (Content Delivery Network) to include jQuery directly in your HTML without downloading it.
  2. Save jQuery File: Save the downloaded jQuery file to a directory on your computer. Make note of the file path.
  3. Include jQuery in HTML: Open your HTML file in a text editor and add the following code inside the <head> section:
HTML
<!DOCTYPE html>
<html>
  <head>
    <title>jQuery Example</title>
    <script src="path/to/jquery.js"></script>
  </head>
  <body>
    <!-- Rest of your HTML content -->
  </body>
</html>

Replace "path/to/jquery.js" with the actual file path of the jQuery file you downloaded or the CDN URL if you’re using a CDN.

  1. Use jQuery: Once jQuery is included in your HTML file, you can start using its functionality by writing JavaScript/jQuery code within <script> tags, either in the <head> section or before the closing </body> tag.

Here’s an example of using jQuery to change the text of an element:

HTML
<!DOCTYPE html>
<html>
  <head>
    <title>jQuery Example</title>
    <script src="path/to/jquery.js"></script>
    <script>
      $(document).ready(function() {
        // jQuery code here
        $("h1").text("Hello, jQuery!");
      });
    </script>
  </head>
  <body>
    <h1>Original Text</h1>
  </body>
</html>

In the above example, we include jQuery using the <script> tag, and then use $(document).ready() to ensure that the jQuery code runs after the HTML document has finished loading. Inside the function, we select the <h1> element using the jQuery selector $("h1") and change its text using the text() method.

Remember to adjust the file path or CDN URL to match your jQuery source location.

That’s it! You have now successfully added jQuery to your HTML document and can start using its powerful features.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS