jQuery ready() functionThe ready() function in jQuery executes the code only when the DOM (Document object model) is fully loaded. It is an inbuilt function in jQuery. It can fire before loading of all images, etc. but only when the DOM is ready. The code inserted between $(document).ready() is executed only when the page is ready for JavaScript code to execute. We should not use the ready() method together with the <body onload = "">. SyntaxWe 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. Parameter ValuesThe 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. ExampleIn 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 NowOutput: After the execution of the above code, the output will be - After clicking the given button, the output will be - Next TopicjQuery removeAttr() method |