To use jQuery's .load() method for loading content into an element, follow these steps:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="content"></div>
$(document).ready(function(){
$("#content").load("external.html", function(){
console.log("Content loaded successfully!");
});
});
The "external.html" is the URL of the external HTML file you want to load. You can also load content from other types of files like plain text, JSON, or XML.
Note: Make sure the JavaScript code is placed within the <script>
tag and either inside the <head>
section (wrapped within $(document).ready(function(){ ... })
) or just before the closing </body>
tag to ensure the DOM elements are available before trying to load content into them.