Different ways of including jQuery in HTML
Direct Download:
You can download the jQuery library from the official website (https://jquery.com/) and include it in your HTML file using the <script> tag.
CDN (Content Delivery Network):
You can include jQuery directly from a CDN, such as Google CDN or jQuery’s own CDN. This method offers the advantage of faster loading times and better caching.
CDN link:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Package Manager:
If you’re using a package manager like npm or Yarn, you can install jQuery as a dependency and include it in your project.
command:
npm install jQuery
Example: In this example, when the button is clicked, the paragraph element (<p>) is toggled between visible and hidden states.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<h2>jQuery Example</h2>
<button>Toggle Paragraph</button>
<p>This is a paragraph with some text.</p>
</body>
</html>
Output: