The jQuery document ready event prevents any jQuery code from running while your page is loading. If you use jQuery without it, so it may be possible that your script will not work.



The best example can suppose your code is written in the head section and executes to change something in the footer section, but the footer section is not fully loaded; in this case, your code will not work.

Syntax:

$(document).ready(function() {

    // write your code here...

});

or you can also use an even shorter method.

$(function() {

    // write your code here...

});

An Example of Document Ready Method

Example:

$(document).ready(function() {
    $("div").click(function() {
        $(this).addClass("selected");
    });
});

In the above example, clicking on the div tag will add a newly selected class.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram