Do you use the .ready() function?

Loading last updated info...

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

Authors

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star