JQuery Document Ready
JQuery's document ready
function is a useful tool for executing JavaScript code when the DOM is fully loaded and ready for manipulation.
Syntax
The basic syntax for using the document ready
function in JQuery is:
$(document).ready(function() {
// Your JavaScript code goes here
});
Alternatively, you can use a shorthand notation for the document ready
function:
$(function() {
// Your JavaScript code goes here
});
Both of these syntax options accomplish the same thing - they wait for the DOM to be fully loaded before executing the JavaScript code within the function.
Use
The document ready
function is particularly useful for manipulating the DOM with jQuery, as you can be sure that all the necessary elements are loaded before attempting to modify their properties, values, or attributes. It is also useful for binding events to elements, such as clicking, hovering, or scrolling.
Example
Here is an example of how to use the document ready
function in JQuery: