The jQuery load()
method is used to load data from the server and place the returned HTML into the selected element without refreshing the page.
The basic syntax of the load()
method is as follows:
$(selector).load(url, [data], [callback]);
Here, selector
is the DOM element to load the content into. url
is the URL of the server-side script to send the request to. [data]
is an optional parameter that can be used to send data to the server along with the request. [callback]
is an optional function that is called when the request completes.
For example, to load the content of a server-side script example.php
into a div
element with the id
“result”, you can use the following code:
$("#result").load("example.php");
You can also send data to the server using the data
parameter. For example, to send the value of an input field with the id
“name” to the server, you can use the following code:
$("#result").load("example.php", {name: $("#name").val()});
When the request completes, you can perform additional actions using the callback
function. For example, to display an alert when the request completes, you can use the following code:
$("#result").load("example.php", function() {
alert("Request complete.");
});
Note that the load()
method can also be used to load other types of data, such as JSON or plain text, by specifying the appropriate dataType
parameter.