How to use innerHTML in JavaScript ? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report The innerHTML property in JavaScript allows you to get or set the HTML content of an element as a string. Syntax ForGetting HTML content of an element:let element = document.getElementById("myElementId"); let htmlContent = element.innerHTML;Setting HTML content of an element:let element = document.getElementById("myElementId"); element.innerHTML = "New HTML content";Here are step-by-step instructions on how to use the innerHTML property in JavaScriptStep 1: Access an HTML elementUse JavaScript to select the HTML element you want to manipulate. You can do this using various methods like document.getElementById(), document.querySelector(), or document.querySelectorAll(). <div id="myElement">Initial content</div>let element = document.getElementById("myElement");Step 2: Get the HTML content If you want to retrieve the current HTML content of the element, use the innerHTML property. let htmlContent = element.innerHTML; console.log(htmlContent); // Output: Initial contentStep 3: Set the HTML content If you want to replace or update the HTML content of the element, assign a new HTML string to the innerHTML property. element.innerHTML = "<b>New content</b>";Step 4: Verify the changes(optional)You can optionally verify that the HTML content has been updated as expected by accessing the element again or using browser developer tools. console.log(element.innerHTML); // Output: <b>New content</b>Example: Here, we have show the code on how to use innerHTML in JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>innerHTML Example</title> <style> .container { padding: 10px; } </style> </head> <body> <div class="container" id="myElement">Initial content</div> <button onclick="changeContent()">Change Content</button> <script> function changeContent() { let element = document.getElementById("myElement"); element.innerHTML = "<b>New content</b>"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to use innerHTML in JavaScript ? A amanv09 Follow Improve Article Tags : JavaScript Web Technologies javaScript Similar Reads How to use Ejs in JavaScript ? EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML. Installation StepsInstall the module using the followin 3 min read JavaScript innerHTML The innerHTML property in JavScript is used to append the dynamic HTML or text content to an element using JavaScript. It is designed to add dynamic HTML, but developers also use it to add text content as well. It can be directly used with the element by selecting it using DOM manipulation. Syntax:s 2 min read How to Make a Breakline in JavaScript for innerHTML? In JavaScript, while working with HTML you will frequently need to add line breaks whether building a complex or simple web application. In this article, you will learn to insert line breaks in JavaScript's innerHTML.These are the following approaches: Table of Content Using < br > TagUsing Te 2 min read How to include inline JavaScript in HAML ? There are mainly two ways to include JavaScript in HTML: Internal/Inline: By using "script" element in the "head" or "body" section. External: By using an external JavaScript file. We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inli 2 min read What is the Disadvantage of using innerHTML in JavaScript ? The innerHTML property is a part of the Document Object Model (DOM) that is used to set or return the HTML content of an element. Where the return value represents the text content of the HTML element. It allows JavaScript code to manipulate a website being displayed. More specifically, it sets or r 3 min read JavaScript HTML DOM The JavaScript HTML DOM (Document Object Model) is a powerful tool that represents the structure of an HTML document as a tree of objects. It allows JavaScript to interact with the structure and content of a webpage. By manipulating the DOM, you can update the content, structure, and styling of a pa 4 min read How to Display Images in JavaScript ? To display images in JavaScript, we have different approaches. In this article, we are going to learn how to display images in JavaScript. Below are the approaches to display images in JavaScript: Table of Content Using CreateElementUsing InnerHTMLApproach 1: Using CreateElementIn an HTML document, 2 min read HTML content modification using JavaScript JavaScript is the dynamic, lightweight, and most common computer programming language used to create web pages. It interacts with client-side and makes dynamic pages. JavaScript Can Change the Content of an HTML page. The getElementById() method is used to get the id of the element and change the HT 2 min read How to Add JavaScript in HTML Document? To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev 3 min read How does inline JavaScript work with HTML ? Inline JavaScript refers to JavaScript code embedded directly within HTML elements using the onclick, onmouseover, or other event attributes. This allows you to execute JavaScript code in response to user interactions or specific events without needing a separate JavaScript file or script block.Synt 2 min read Like