In JavaScript, jQuery is a powerful and widely used library to simplify common web scripting tasks. It is a lightweight, fast, and feature-rich JavaScript library that is built on the principle ‘write less, do more. Its easy-to-use APIs make the things such as HTML document traversal and manipulation, adding animation effects, and event handling to a web page easier to work seamlessly across all the major browsers including Firefox, Chrome, Internet Explorer, Safari, and so on.
jQuery also makes it possible to create an Ajax based application quickly and simply. large companies like Microsoft, Google, and IBM are making use of jQuery for their applications. This shows you how popular and powerful the jQuery is?
In 2006 jQuery was originally created by John Resig. The project is currently run and maintained by a decentralized group of developers as an open-source project.
when you're not familiar with jQuery, you might be imagining what makes jQuery so special. Below are the advantages and the reasons you should opt for jQuery:
To get started with jQuery, you have to download a copy of jQuery first and include it in your document. jQuery has versions available for downloading and they are ‘compressed’ and ‘uncompressed’.
However, the uncompressed file is best suited for development or debugging. while the minified and compressed file is preferred for production because it saves the precious bandwidth and improves the performance due to the small file size.
You can download jQuery from here: https://jquery.com/download/
Once done with downloading the jQuery file you can see it has a '.js’ extension because the jQuery is just a JavaScript library, so you can include the jQuery file in your HTML document with the <script> element just like you include normal JavaScript files. Below is an example of how to add jQuery:
Example how to add jquery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple HTML Document</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Hello, Developers!</h1>
</body>
</html>
Alternatively, jQuery can be included in your document through freely available CDN (Content Delivery Network) links, if you can’t and host jQuery yourself. CDNs can offer a performance benefit, as it reduces the loading time; this is because they are hosting jQuery on multiple servers spread across the globe and when a user requests the file, it will be served from the server nearest to them.
Also, it offers an advantage that if the visitor to your webpage has already downloaded a copy of jQuery from the same CDN while visiting other sites, it won't be re-downloaded as it is already there in the browser's cache.
Jquery CDN
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
Creating Your First Web page with jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>First Web page with jQuery</title>
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$("h1").css("color", "#0088ff");
});
</script>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
From the above example, we've performed a simple jQuery operation by changing the color of the heading (i.e the <h1> element) by using the jQuery element selector and css() method when the document is ready which is known as document ready event. Next, you will learn about jQuery selectors, events, and methods in subsequent tutorials.