Skip to Content

How do I change the inner HTML element?

Changing the inner HTML element allows you to dynamically update the content of an HTML element on a web page. This can be useful for creating dynamic and interactive web pages that change content on the fly without having to reload the entire page.

What is Inner HTML?

The inner HTML of an HTML element refers to the HTML content within that element. For example:

<div id="myDiv">
  This is the inner HTML
</div>

The inner HTML of the div element above is “This is the inner HTML”.

Changing the inner HTML allows you to modify this content dynamically.

Why Change Inner HTML?

There are several reasons you may want to change the inner HTML of an element:

  • To update content on a page without reloading
  • To dynamically display new information as it becomes available
  • To implement interactive UI elements like accordions, tabs, popups, etc
  • To swap out images, text, videos, etc on the fly

By changing the inner HTML with JavaScript, you can create web pages that change and adapt to user input and interactions for a smooth and dynamic experience.

How to Change Inner HTML

There are a few different ways to modify the inner HTML of an element with JavaScript:

innerHTML Property

The simplest way is to use the innerHTML property:

document.getElementById("myDiv").innerHTML = "New content!"; 

This finds the element with id=”myDiv” and sets its inner HTML to the new string “New content!”.

Element.insertAdjacentHTML()

The insertAdjacentHTML() method allows you to insert HTML content into an element:

let newContent = "<p>This paragraph is inserted</p>";

document.getElementById("myDiv").insertAdjacentHTML('beforeend', newContent);

This would insert the newContent HTML into the end of the myDiv element.

document.write()

The document.write() method allows you to write HTML content into the page:

document.write("<h2>New Heading</h2&gt");

This would directly write a new H2 heading onto the page.

Note: document.write() can cause issues if used after the page has finished loading.

Full Example Changing Inner HTML

Here is a full example demonstrating how to change the inner HTML of an element:

<!DOCTYPE html>
<html>
<body>

<div id="myDiv">
  <p>Original paragraph</p>
</div>

<script>
  // Change inner HTML using innerHTML property
  document.getElementById("myDiv").innerHTML = "New content!";
  
  // Insert HTML using insertAdjacentHTML
  let newParagraph = "<p>A new paragraph!</p>";
  document.getElementById("myDiv").insertAdjacentHTML('beforeend', newParagraph);
  
  // Directly write HTML using document.write
  document.write("<p>This paragraph is written directly</p>");
</script>

</body>
</html>

This example shows:

  • Changing the inner HTML completely using innerHTML
  • Inserting new HTML content with insertAdjacentHTML()
  • Writing direct HTML using document.write()

Which results in this output:

New content!

<p>A new paragraph!</p>

<p>This paragraph is written directly</p>

The original paragraph is replaced, a new paragraph is inserted, and a paragraph is written directly onto the page.

Inner HTML Use Cases

Some common use cases for modifying inner HTML include:

Dynamically Updating Content

You can update text, images, videos, etc on a page by changing the innerHTML as needed:

let img = document.getElementById("headerImg");
img.src = "new-header.jpg"; 

This would swap out the header image without reloading the page.

Creating Interactive Elements

You can create accordions, tabs, popups, etc by toggling inner HTML:

function togglePopup() {
  let popup = document.getElementById("popup");
  popup.innerHTML = "<p>Hello World!</p>"; 
}

This would show/hide a popup by adding/removing its inner HTML.

Dynamic Forms

You can update forms and form data by changing the innerHTML of the form elements.

Filtering and Templating

You can filter and template dynamic data client-side by generating HTML strings and setting the innerHTML.

Important Notes on Inner HTML

Here are some important notes to keep in mind when working with inner HTML:

  • Changing innerHTML completely replaces the existing content
  • Use insertAdjacentHTML() to insert new content without overwriting existing
  • Using innerHTML can expose you to XSS vulnerabilities if you insert untrusted input
  • Changing the innerHTML will cause any existing event listeners or references to the original DOM nodes to be lost
  • For large changes, modify the DOM directly rather than using innerHTML for better performance
  • Take care with document.write() as it can cause issues if the document has finished loading

Summary

To recap, the key ways to modify inner HTML are:

  • innerHTML – sets the HTML content
  • insertAdjacentHTML() – inserts HTML
  • document.write() – writes HTML content

Changing the inner HTML allows you to dynamically update page content and build interactive web pages. Just be mindful of overusing innerHTML, and prefer direct DOM manipulation for large changes.

Frequently Asked Questions

How do I change the text inside a div element?

To change the text inside a div, get a reference to the div element then set its innerHTML property:

<div id="myDiv">Original Text</div>

<script>
  let div = document.getElementById("myDiv");
  div.innerHTML = "New text!"; 
</script>

Is innerHTML a secure way to add content?

innerHTML can expose cross-site scripting (XSS) vulnerabilities if you insert untrusted user input without sanitizing it first. It’s safer to create elements and insert content via the DOM API rather than using innerHTML in cases where security is a concern.

Can I use innerHTML with form elements?

Yes, you can use innerHTML to modify form input elements like text inputs and textareas. For example:

 
document.getElementById("myInput").innerHTML = "New value";

Just be aware this may clear any existing form data or event listeners on the element.

What’s the difference between textContent and innerHTML?

textContent sets or returns only the text content of an element, without any HTML tags. innerHTML sets or returns the content with HTML formatting intact.

Is it better to use innerHTML or the DOM methods?

For small changes, innerHTML can be quicker and more convenient. But for large scale changes, modifying the DOM directly is usually better for performance. Use DOM methods like appendChild(), removeChild(), etc for large updates rather than setting innerHTML.

Conclusion

Changing the inner HTML of an element is a straightforward way to dynamically update content on a page. The key methods are innerHTML to completely replace content, insertAdjacentHTML() to insert new content, and document.write() to directly write HTML.

Be aware of XSS risks when inserting untrusted data, and use DOM methods instead of innerHTML for large modifications for better performance. Used properly, modifying inner HTML is a useful tool for creating dynamic web pages.