The document ready function is used to make sure that the HTML page is fully loaded before jQuery code runs.
If jQuery code runs before the page loads, it may not find the HTML elements.
This function prevents errors that occur when jQuery tries to access elements that are not yet created.
This is the standard syntax for document ready.
All jQuery code is usually written inside this function.
This is a shorter version of the document ready function.
Both methods work the same and are commonly used by developers.
Here, the click event is added only after the page is loaded.
This avoids errors and ensures smooth execution.
Important Points:
- Always use document ready in jQuery
- Prevents undefined element errors
- Short syntax is widely used
- Very important for beginners