jQuery ready() with Examples

jQueryWeb DevelopmentFront End Technology

The ready() method in jQuery occurs when DOM has been loaded.

Syntax

The syntax is as follows −

$(document).ready(func)

Above, func specifies the function to run after the document is loaded.

Example

Let us now see an example to implement the jQuery ready() method 

 Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
div {
   width:600px;
}
.demo * {
   display: block;
   background-color;
   border: 2px solid yellow;
   color: blue;
   padding: 10px;
   margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("li.test").siblings().css({"background-color": "orange","color": "white", "border": "3px dashed blue"});
   });
</script>
</head>
<body>
<h2>Demo Heading</h2>
<div class="demo">
<ul>ul (parent)
<li>previous sibling</li>
<li>previous sibling</li>
<li>previous sibling</li>
<li class="test">li (sibling)</li>
<li>next sibling</li>
<li>next sibling</li>
<li>next sibling</li>
</ul>
</div>
</body>
</html>

Output

This will produce the following output−

Example

Let us see another example −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("p").slideToggle();
      });
   });
</script>
</head>
<body>
<h2>Exam Info</h2>
<p>Examination begins on 24th December. The students are requested to submit project report before 15th December.</p>
<button>Toggle</button>
</body>
</html>

Output

This will produce the following output −

Now, click “Toggle” to toggle the info −

On clicking “Toggle” again, the info will be visible −

raja
Published on 31-Dec-2019 12:21:24
Advertisements