load() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:load

jQuery load() Method

Description

The load() method loads data from a server and sets the returned data into the selected element.

Syntax

$(selector).load(url,data,function(response,status,xhr))
Parameter RequireDescription
url Required.URL to load
data Optional. data to send to the server along with the request
function(response,status,xhr) Optional.function to run when the load() method is completed.

Additional parameters:

  • response - result data from the request
  • status - status of the request ("success", "notmodified", "error", "timeout", or "parsererror")
  • xhr - XMLHttpRequest object

The following code shows how to load the content of the file "demo_test.txt" into a specific <div> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#div1").load("demo_test.txt");
    });// w  w  w .java  2  s. com
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Load</button>

</body>
</html>

Related Tutorials