jQuery load() with Examples


The load() method in jQuery is used to load data from a server and puts the returned data into the selected element

Syntax

The syntax is as follows −

$(selector).load(url,data,function(response,status,xhr))

Above, URL is the URL to be loaded. The data parameter is the data to send to the server, whereas the callback function runs when the load() completes.

Example

Let us now see an example to implement the jQuery load() method 

 Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("div").load("new.txt");
      });
   });
</script>
</head>
<body>
<div>
<p>Demo text...</p>
</div>
<button>Load Content from server</button>
</body>
</html>

Output

This will produce the following output −

Click the above button to load content from “new.txt” −

The content visible above is the new.txt file.

Updated on: 31-Dec-2019

555 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements