Menu Close

How to use $( document ).ready() method in jquery?

ready-method

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> 
Posted in HTML, jQuery, Web Technologies

You can also read...