← Back to Chapters

JavaScript DOM innerHTML

? JavaScript DOM innerHTML

⚡ Quick Overview

The innerHTML property of a DOM element allows you to get or set the HTML markup inside that element. Using innerHTML, you can dynamically change the content and structure of a webpage with JavaScript.

It is one of the most commonly used properties when working with the DOM because it lets you insert, replace, or append HTML content as a string.

? Key Concepts

  • Read content: You can read the current HTML content of an element using element.innerHTML.
  • Replace content: Assigning a new string to innerHTML completely replaces the existing content.
  • Append content: Using += with innerHTML lets you add new HTML without removing the old content.
  • HTML markup support: The string you assign can contain tags like <p>, <strong>, <li>, etc.
  • DOM updates: When innerHTML changes, the browser rebuilds the DOM inside that element.

? Syntax & Theory

Basic syntax:

  • element.innerHTML → returns the HTML markup inside the element as a string.
  • element.innerHTML = "new HTML" → replaces the existing HTML inside the element.

Since innerHTML works with strings, the HTML tags inside must be written as a valid HTML string. When working inside JavaScript, you usually need to escape the angle brackets using entities like &lt; and &gt; if you are writing them inside another HTML document.

? Code Examples

? Example: Get and Set innerHTML

? View Code Example (Read & Write)
// Get the current HTML content
const container = document.getElementById("container");
console.log(container.innerHTML);
// Set new HTML content
container.innerHTML = "<p>New <strong>HTML</strong> content</p>";

? Example: Adding Elements with innerHTML

? View Code Example (Append List Items)
const list = document.getElementById("myList");
list.innerHTML += "<li>New Item</li>";

? Example: Modify Content with innerHTML

HTML:

? View HTML Markup
<div id="container">
<p>Old content</p>
</div>

JavaScript:

? View JavaScript
const container = document.getElementById("container");
container.innerHTML = "<h2>Updated Content</h2><p>This is new content.</p>";

? Live Output & Explanation

Below is a simple interactive example. Click the button to update the content of the box using innerHTML.

? Original Content

This text will be replaced using innerHTML.

When you click the button, JavaScript selects the <div> with id="demoBox" and replaces its entire inner HTML with a new heading and paragraph. This demonstrates how innerHTML can change both text and structure at once.

? Use Cases / When to Use

  • Building or updating small HTML snippets like lists, cards, or message boxes.
  • Loading pre-built HTML templates (as strings) into a container.
  • Quickly prototyping dynamic UIs without complex DOM APIs.
  • Updating content based on user actions (buttons, menus, form submissions).

? Tips & Best Practices

  • Use innerHTML when you need to insert or replace HTML markup dynamically.
  • Remember that setting innerHTML replaces all existing content inside the element.
  • Use innerHTML += ... if you want to append without removing existing content.
  • Be careful with user input to avoid security risks like Cross-Site Scripting (XSS). Never insert untrusted user text directly into innerHTML.
  • For very large or frequent updates, consider using DOM methods like createElement and appendChild for better performance.

? Try It Yourself

  • Use innerHTML to change the structure of a webpage dynamically (e.g., replace a paragraph with a list).
  • Practice adding new elements inside an existing container using innerHTML +=.
  • Observe how changing innerHTML affects the DOM tree using your browser's developer tools.
  • Set innerHTML to an empty string "". What happens to the element's content?
  • Try appending multiple list items at once using one innerHTML assignment.
  • Experiment with updating innerHTML inside a button click event (similar to the demo above).
  • Think about when you would prefer DOM methods (like appendChild) instead of innerHTML.