Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • jQuery - Get Started
  • What is jQuery
  • jQuery - Environment Setup
  • Start using jQuery
  • jQuery - Selectors
  • jQuery - Methods
  • DOM Manipulation
  • Attributes Manipulation
  • Dimensions Manipulation
  • Traversing Elements
  • CSS Manipulation
  • Animation
  • Events
  • AJAX Introduction
  • AJAX Methods
  • Get Method
  • Post Method
  • Load Method
  • Selector Reference
  • DOM Manipulation Methods
  • Traversing Methods
  • Effects Methods
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

jQuery load() Method

The jQuery load() method allows HTML or text content to be loaded from a server and added into a DOM element.

Syntax:
$.load(url,[data],[callback]);

Parameters Description:

  • url: request url from which you want to retrieve the content
  • data: JSON data to be sent with request to the server.
  • callback: function to be executed when request succeeds

The following example shows how to load html content from the server and add it to div element.

Example: Load HTML Content
$$('#msgDiv').load('/demo.html');
    
<div id="msgDiv"></div>
Try it

In the above example, we have specified html file to load from the server and add its content to the div element.

Note : If no element is matched by the selector then Ajax request will not be sent.

The load() method allows us to specify a portion of the response document to be inserted into DOM element. This can be achieved using url parameter, by specifying selector with url separated by one or multiple space characters as shown in the following example.

Example: jQuery load() Method
$$('#msgDiv').load('/demo.html #myHtmlContent');

<div id="msgDiv"></div>

In the above example, content of the element whose id is myHtmlContent, will be added into msgDiv element. The following is a demo.html.

demo.html content:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta property="og:title" content="" />
</head>
<body>
    <h1>This is demo html page.</h1>
    <div id="myHtmlContent">This is my html content.</div>
</body>
</html>

The load() method also allows us to specify data to be sent to the server and fetch the data.

Example: Set Data in load()
$$('#msgDiv').load('getData', // url 
                  { name: 'bill' },    // data 
                  function(data, status, jqXGR) {  // callback function 
                            alert('data loaded')
                    });

<div id="msgDiv"></div>

In the above example, first parameter is a url from which we want to fetch the resources. The second parameter is data to be sent to the server. The third parameter is a callback function to execute when request succeeds.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.