jQuery - ready( fn ) Method


Advertisements


Description

The ready( fn ) method binds a function to be executed whenever the DOM is ready to be traversed and manipulated.

This method is a replacement for using window.onload

Syntax

Here is the simple syntax to use this method −

selector.ready( fn )

Parameters

Here is the description of all the parameters used by this method −

  • fn − The function to be executed when the DOM is ready.

Example

Following is a simple example a simple showing the usage of this method −

Live Demo
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("div").text("The DOM is now loaded...");
         });
      </script>
		
      <style>
         .div{ margin:10px;padding:12px; border:2px solid #666; width:200px;}
      </style>
   </head>
	
   <body>
      <div class = "div" style = "background-color:blue;"></div>
   </body>
</html>

This will produce following result −


jquery-events.htm

Advertisements