Do you use the .ready() function?

Updated by Brady Stroud [SSW] 1 year ago. See history

123

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: Figure: Bad example - If this jQuery is in the wrong place, the #login element may not exist!

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

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

acknowledgements
related rules