Home
Do you use the .ready() function?
  v2.0 Posted at 11/09/2015 9:22 AM by William Yin
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

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: