Do you use the .ready() function?

Last updated by Brady Stroud [SSW] about 2 months ago.See history

Putting your initialization JavaScript code inside the .ready() function is not always required, but it's much safer to do so.

jQuery exposes a .ready() event which fires when the Document Object Model (DOM) is fully loaded and ready to be manipulated.

You can attach a function to this event so you can be sure the page is ready for you to work on.

$("#login").addClass("hidden");

Figure: Bad example - If this jQuery is in the wrong place, the #login element may not exist!

$(function () {
  $("#login").addClass("hidden");
});

Figure: Good example - This code won't run until the DOM is fully loaded

We open source. Powered by GitHub