In this article, we will see how to run a code when a page loads using jQuery. To run a code at page load time, we will use the ready() method. This method helps to load the whole page and then execute the rest code. This method specifies the function to execute when the DOM is fully loaded.
Syntax:
$(document).ready(function)
Parameters: This method accepts a single parameter function which is mandatory. It is used to specify the function to run after the document is loaded.
Return Value: This method returns the document after performing the ready() method.
Example: In this example, we will use the ready() method.
HTML
<!DOCTYpe html>
< html >
< head >
< title >
How to run a code on document
ready event in jQuery?
</ title >
< script src =
</ script >
< script >
$(document).ready(function () {
$("body").css("text-align", "center");
$("h1").css("color", "green");
$("p").css("font-size", "14px");
$("p").css("font-weight", "bold");
});
</ script >
</ head >
< body >
< h1 >GeeksforGeeks</ h1 >
< h3 >
How to run a code on document
ready event in jQuery?
</ h3 >
< p >GeeksforGeeks: A computer science portal</ p >
</ body >
</ html >
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 May, 2023
Like Article
Save Article