Load() Method in jQuery


The load() method used to loads data from a server and returned to selected element.

Example :

The following example show, how to load data from sample.txt file when clicked the button using jQuery load() method.

 
<html>
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <body>
    <input type='button' value='Load Data' id='btn'>
    <div id='res'>
    </div>
  </body>
  <script>
    $(document).ready(function(){
      $("#btn").click(function(){
        $("#res").load("sample.txt",function(response, status, xhr){
          if(status=="error"){
            $("#res").html(xhr.statusText);
          }
        });
      });
    });
  </script>
</html>
Try it Yourself
sample.txt
<h1 style='font-size:20px;'>Hello World</h1>
<p style='color:green;'>Sample paragraph  from sample.txt file</p>
<ul>
  <li>List Item 1</li>
  <li>List Item 2</li>
  <li>List Item 3</li>
  <li>List Item 4</li>
  <li>List Item 5</li>
</ul>

Demo :