The $(document).ready() is used to make a function of our codes to execute after the document is loaded. We have to write our codes inside the $(document).ready() method. It will run once the page DOM is ready to execute JavaScript code.
We can use two syntax in this method.
Syntax:
$(document).ready(function)
The ready() method is used only for current documents. So the selector is not required. We can also replace the above syntax as follows. These two syntax are the same and we can use either one of these.
$(function)
Follow the below example to learn about how to use $(document).ready() method in jquery.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>$( document ).ready() method in jquery</title>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
$(document).ready(function(){
$(".ready-method").click(function(){
alert("Hello, world!");
});
});
</script>
</head>
<body>
<div class="ready-method">
<button>Click me!</button>
</div>
</body>
</html>
Popular Posts
- Show / Hide div based on dropdown selected using jQuery
- Autosuggestion Select Box using HTML5 Datalist, PHP and MySQL with Example
- Custom Authentication Login And Registration Using Laravel 8
- Infinite Scrolling on PHP website using jQuery and Ajax with example
- How to Convert MySQL Data to JSON using PHP
- Google Login or Sign In with Angular Application
- How to change date format in PHP?
- Image Lazy loading Using Slick Slider
- Slick Slider Basic With Example
- php in_array check for multiple values
- Adaptive Height In Slick Slider
- Slick Slider Center Mode With Example
- How to Scroll to an Element with Vue 3 and ?
- JavaScript Multiple File Upload Progress Bar Using Ajax With PHP
- Slick Slider Multiple Items With Example
Total Views: 749