1. Back To Blog

What is the use of Document.ready in Jquery ? ( ASP.NET Jquery interview questions with answers)

Do not forget to visit www.questpond.com for more such ASP.NET and jquery interview questions with answer videos.

 

"Document.Ready" event occurs once the complete HTML DOM is loaded. So the next question is when do we actually need this event?.Consider the below simple code where we are trying to set a text box "text1" with value "Sometext".

 

Now at the point when Jquery code tries set the textbox value , at that moment that text box is not available in the HTML DOM. So it throws an exception for the same.

 

<script>

      $("#text1").val("Sometext"); // Throws exception as the textbox is not //accessible at this moment

  </script>

</head>

<body>

<input type="text" id="text1" />

</body>

 

So we would like to execute the Jquery code which sets the textbox value only when all the HTML objects are loaded in DOM. So you can replace the code of setting text box value to something as shown below.

 

<script>

       $(document).ready(function()

           $("#text1").val("Sometext");

       });

  </script>

 

Also do read this Javascript Interview questions with answers :- How to debug Javascript ?

 

Below is a nice ASP.NET interview question , Do session use cookies ?

 

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social