jQuery ready() function
We should not use the ready() method together with the <body onload = "">.
We can use the ready() function in two ways, as given below.
Both of the above ways are valid to use. The second syntax can be used as an alternative to the first syntax. It can be clear from the following piece of code.
The above code is equivalent to the below code.
Some experienced developers use the shorthand $() rather than using $(document).ready(), but If we are writing the code for non-experienced people, so it's better to use the long-form.
The ready() function has only a single parameter value defined as follows.
function(): It is the mandatory parameter that specifies the function which executes after the document is loaded.
Let's see an example of using the $(document).ready() function.
In this example, there are some paragraph elements and a button. Here, we are using $(document).ready() function to change the style of the given paragraphs.Test it Now
After the execution of the above code, the output will be -
After clicking the given button, the output will be -