Questions And Answers

More Tutorials

Jquery document-ready event

jQuery code is often wrapped in jQuery(function($) { ... }); so that it only runs after the DOM has finished loading.

<script type="text/javascript">
 jQuery(function($) {
 // this will set the div's text to "Hello".
<div id="myDiv">Text</div>

This is important because jQuery (and JavaScript generally) cannot select a DOM element that has not been rendered to the page.

<script type="text/javascript">
 // no element with id="myDiv" exists at this point, so $("#myDiv") is an
 // empty selection, and this will have no effect
<div id="myDiv">Text</div>

Note that you can alias the jQuery namespace by passing a custom handler into the .ready() method. This is useful for cases when another JS library is using the same shortened $ alias as jQuery, which create a conflict. To avoid this conflict, you must call $.noConflict(); - This forcing you to use only the default jQuery namespace (Instead of the short $ alias).

By passing a custom handler to the .ready() handler, you will be able to choose the alias name to use jQuery.

jQuery( document ).ready(function( $ ) {
 // Here we can use '$' as jQuery alias without it conflicting with other
 // libraries that use the same namespace
jQuery( document ).ready(function( jq ) {
 // Here we use a custom jQuery alias 'jq'

Rather than simply putting your jQuery code at the bottom of the page, using the $(document).ready function ensures that all HTML elements have been rendered and the entire Document Object Model (DOM) is ready for JavaScript code to execute.


In this page (written and validated by ) you learned about Jquery document ready event . What's Next? If you are interested in completing Jquery tutorial, your next topic will be learning about: Jquery Attaching events and manipulating the DOM inside ready.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.