How to Load DIV Content on Click Only using JavaScript? Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To load content into a div when a button or any other element is clicked using JavaScript, you can follow these simple steps. You can either load static content or fetch data from an external source using AJAX (for more advanced usage). Below is a basic example to load content into a div when a button is clicked.Basic Example: Loading Static Content on ClickHTML Structure: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Load Content on Click</title> </head> <body> <!-- Button to trigger the content load --> <button id="loadBtn">Load Content</button> <!-- Div where the content will be loaded --> <div id="contentDiv"> <!-- Content will be loaded here --> </div> <!-- Link to the JavaScript file --> <script src="script.js"></script> </body> </html> JavaScript (script.js): JavaScript // Get the button and content div elements const loadButton = document.getElementById("loadBtn"); const contentDiv = document.getElementById("contentDiv"); // Add an event listener to the button loadButton.addEventListener("click", function() { // Load static content into the div contentDiv.innerHTML = "<h2>New Content Loaded!</h2><p>This is the new content added when the button was clicked.</p>"; }); Output:OutputOnclickExplanation:HTML:There's a button (<button>) that will trigger the action to load content.The content will be inserted into a <div> with the id="contentDiv".JavaScript:We first get references to the button and the div where the content will be loaded.We use addEventListener to detect a click on the button.When the button is clicked, we change the innerHTML of the div to insert new content.Example: Loading Content from a File or External SourceIf you want to load content from a file (e.g., a text file or a JSON file) when the button is clicked, you can use fetch() or XMLHttpRequest. Here's an example using fetch() to load text content from a file.Updated JavaScript (script.js) to load external content:Explanation for External Content:Fetch API: The fetch() function makes an HTTP request to load content from the file content.txt (you can replace it with your file path or URL).Handling the Response: When the content is successfully fetched, it’s inserted into the div. If there's an error (e.g., the file doesn't exist), an error message is shown instead.Conclusion:For basic content changes, you can simply modify the innerHTML of a div.For dynamic content, consider using fetch() or XMLHttpRequest to load content from external files or APIs.This approach will make sure the content inside the div is updated only when the button is clicked Comment More infoAdvertise with us Next Article How to Load DIV Content on Click Only using JavaScript? A anuragtriarna Follow Improve Article Tags : JavaScript Web Technologies Web QnA Similar Reads How to Open a Popup on Click using JavaScript ? Opening a Pop-up when the click action is performed is the user-interactive technique. We can use this functionality in messages, or forms to show some error or alert messages before the user moves to the next action.Table of ContentUsing display propertyUsing classList PropertyUsing visibility prop 4 min read How to make HTML table expand on click using JavaScript ? The expandable table can be achieved by using JavaScript with HTML. By Clicking on a row of the table, it expands and a sub-table pops up. When the user again clicks on that row the content will hide. This can be very useful when the data is complex but it is inter-related.Example 1: The following e 5 min read Print the content of a div element using JavaScript If you want to print the content of a <div> element using JavaScript, you can achieve this by extracting the content and then opening a new window or popup to display and print it. This method involves capturing the content of the <div>, creating a new window, and using the print command 2 min read How to Open a Popup on Hover using JavaScript ? We will be learning about the ways to open a pop-up when the cursor hovers over the HTML Element. Hover is a useful UI interaction that helps in getting more information about a specific HTML element. Pop Up can be shown as the additional data to make things much more understandable. It responds to 4 min read How to Open a Popup on Hover using JavaScript ? In JavaScript, to perform the functionality of opening the popup on hover, we can dynamically manipulate the DOM by responding to the mouse events. The below approaches can be used to accomplish this task. Table of Content Using mouseover and mouseout eventsUsing onmouseenter and onmouseleave events 3 min read How to Open a Link Without Clicking on it using JavaScript? To open a link without clicking on it we can use the onmouseover method of JavaScript. The link will open when the mouse moves over the text. It returns a newly created window, or NULL if the call gets failed. Syntax:window.open( URL, name, Specs )Note: Allow Pop-up of Web Browser. Example 1: URL is 1 min read How to add a tooltip to a div using JavaScript? Adding tooltip to div element displays a pop-up, whenever the mouse hovers over div. Syntax: html < div title = "" > </div> <script> $(document).ready(function() { $('[data-toggle="tooltip"]').tooltip(); }); </script> Tooltip Methods: .tooltip("show"): It 2 min read How to Download PDF File on Button Click using JavaScript ? Sometimes, a web page may contain PDF files that can be downloaded by the users for further use. Allowing users to download the PDF files can be accomplished using JavaScript. The below methods can be used to accomplish this task. Table of Content Using html2pdf.js libraryUsing pdfmake LibraryUsing 5 min read How to append HTML code to a div using JavaScript ? The ability to append HTML code to a <div> Using JavaScript helps enhance user experience by enabling dynamic content updates without reloading the page. This technique is widely usedâover 85% of modern websites implement JavaScript DOM manipulation to create smoother and more interactive web 6 min read How to Toggle between Hiding and Showing an Element using JavaScript ? Toggle between hiding and showing an element using JavaScript provides the feature of efficient space usage by allowing users to hide content sections they may not need to minimize distractions, leading to a cleaner and more organized layout. Syntaxif (paragraph.style.display === 'none') { paragraph 3 min read Like