jQuery | $( document ).ready()

When we working with jQuery library most of the time forget to add jQuery code inside $(document).ready() function. jQuery code should load when DOM is ready so you should call it inside the $( document ).ready(). jQuery script will load when DOM is ready and before complete page load.

Find the below two methods to make your jQuery load when DOM is ready.

// $( document ).ready() block.
$( document ).ready(function() {
  //Write your code here
});

Here we use the shorthand $() for $( document ).ready().

// Shorthand for $( document ).ready()
$(function() {
  //Write your code here
});

Example: Adding a class to div using jQuery

$(function() {
  $('div').addClass('newclassname');
});
shanidkv's picture