jQuery Event - ready() Method

Example

Activate the function after the document is loaded:

$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").slideToggle();
  });
});

Try It Yourself

Definition and Usage

The ready event occurs when the DOM (Document Object Model) has been loaded, and the page (including images) has been fully displayed.

Since this event occurs after the document is ready, it is a good practice to place all other jQuery events and functions within this event, as shown in the example above.

The ready() function specifies the code to be executed when the ready event occurs.

The ready() function can only be used for the current document, so no selector is needed.

The following three syntaxes can be used:

Syntax 1

$(document).ready(function)

Syntax 2

$().ready(function)

Syntax 3

$(function)
Parameters Description
function Required. Specifies a function to be executed after the document is loaded.

Tips and Comments

Tip:The ready() function should not be used with <body onload="">.