jQuery load() with Examples

jQueryWeb DevelopmentFront End Technology

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.

raja
Published on 31-Dec-2019 08:03:38
Advertisements