document ready jquery | How and when to use $(document).ready(function()

In order to ensure that jquery code runs all page elements or document should be loaded properly or otherwise code might not execute properly. To ensure document is loaded fully jQuery provides a method to detect this state by way of:

$(document).ready(function()

means document is ready to execute javascript/jquery code. This might be required in scenarios like hiding some data and only showing when user clicks, getting image sizes etc. This would allow your JS code to be in header section of document.

See the example below how to use it:

LIVE DEMO




Was this article helpful?

Related Articles

Leave A Comment?