Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to call a jQuery library function?
Calling a jQuery library function is quite easy. You need to use the script tag to include the jQuery library. As almost everything we do when using jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is ready.
If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.
Using the Document Ready Function
To ensure your jQuery code executes when the DOM is fully loaded, we register a ready event for the document as follows ?
$(document).ready(function() {
// do stuff when DOM is ready
});
This is the foundation for all jQuery operations, as it ensures the HTML elements exist before your JavaScript tries to manipulate them.
Including jQuery Library
To call upon any jQuery library function, you must first include the jQuery library using HTML script tags. You can either download jQuery locally or use a CDN (Content Delivery Network) link.
Complete Example
Here's a complete example showing how to include jQuery and use a library function ?
<html>
<head>
<title>jQuery Function</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function() {
alert("Hello, world!");
});
});
</script>
</head>
<body>
<div id="mydiv">
Click on this to see a dialogue box.
</div>
</body>
</html>
In this example, we use the $("div").click() jQuery function to attach a click event handler to all div elements. When clicked, it displays an alert dialog.
Conclusion
Calling jQuery library functions requires including the jQuery library via script tags and wrapping your code in the $(document).ready() function to ensure proper DOM loading.
